20. marts 2005 - 20:42
Der er
8 kommentarer og 1 løsning
Centrere dialogboks
Jeg vil have en dialogboks centreret ift. en parent frame. Jeg har kigge på mange eks., men jeg kan ikke få nogle af dem til at virke. Hjælp!
Annonceindlæg fra Cornerstones
20. marts 2005 - 20:47
#1
Har lavet en lille metode til det: public static void centerDialog (Component owner, JDialog dialog) { dialog.setLocation(owner.getX() + (owner.getWidth() / 2) - (dialog.getWidth() / 2), owner.getY() + (owner.getHeight() / 2) - (dialog.getHeight() / 2)); }
26. marts 2005 - 22:31
#2
Brugbart?
27. marts 2005 - 17:58
#3
Lige meget hvor jeg kalder metoden fra, så får jeg fejl. Nu har jeg set mig blind på det. Giv mig venligst et hint. Dialogboksens kode følger herunder: ----------------------------------- package elevdatabase; import java.awt.*; import javax.swing.*; import com.borland.jbcl.layout.VerticalFlowLayout; import com.borland.jbcl.layout.BoxLayout2; import com.borland.jbcl.layout.OverlayLayout2; import java.awt.Toolkit.*; /** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */ public class PrintDialog extends JDialog { JPanel panel1 = new JPanel(); JRadioButton jRadioButton1 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); JRadioButton jRadioButton3 = new JRadioButton(); JRadioButton jRadioButton4 = new JRadioButton(); VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout(); GridBagLayout gridBagLayout1 = new GridBagLayout(); JButton jButton1 = new JButton(); public static void centerDialog (Component MainFrame, JDialog PrintDialog) { PrintDialog.setLocation(MainFrame.getX() + (MainFrame.getWidth() / 2) - (PrintDialog.getWidth() / 2), MainFrame.getY() + (MainFrame.getHeight() / 2) - (PrintDialog.getHeight() / 2)); } public PrintDialog(Frame owner, String title, boolean modal) { super(owner, title, modal); try { setDefaultCloseOperation(DISPOSE_ON_CLOSE); jbInit(); pack(); } catch (Exception exception) { exception.printStackTrace(); } } public PrintDialog() { this(new Frame(), "PrintDialog", false); } private void jbInit() throws Exception { panel1.setLayout(verticalFlowLayout1); jRadioButton1.setText("jRadioButton4"); jRadioButton2.setText("jRadioButton3"); jRadioButton3.setText("Alle"); jRadioButton4.setText("jRadioButton2"); this.getContentPane().setLayout(gridBagLayout1); jButton1.setText("Print..."); panel1.setBorder(BorderFactory.createEtchedBorder()); panel1.add(jRadioButton1, null); panel1.add(jRadioButton2, null); panel1.add(jRadioButton3, null); panel1.add(jRadioButton4, null); this.getContentPane().add(jButton1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); this.getContentPane().add(panel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); } }
27. marts 2005 - 19:43
#4
Prøv at bruge min metode direkte: public void centerDialog (Component owner, JDialog dialog) { dialog.setLocation(owner.getX() + (owner.getWidth() / 2) - (dialog.getWidth() / 2), owner.getY() + (owner.getHeight() / 2) - (dialog.getHeight() / 2)); } Du har ændret parameternavnene til at være samme navn som dine klasser. Det tror jeg ikke kan lade sig gøre. Og så vil jeg kalde den fra constructoren: public PrintDialog(Frame owner, String title, boolean modal) { super(owner, title, modal); try { setDefaultCloseOperation(DISPOSE_ON_CLOSE); jbInit(); pack(); centerDialog (owner, this) } catch (Exception exception) { exception.printStackTrace(); } }
27. marts 2005 - 20:25
#5
Dialogboksen kommer bare længere op i venstre øverst hjørne, men når jeg printer getX og getY for owner, så får jeg returneret (0,0). Hvorfor siger den det, når "owner" er i midten af skærmen?
27. marts 2005 - 20:29
#6
Må jeg se den nye kode, og der hvor du kalder den?
28. marts 2005 - 00:22
#7
Her er den: ----------- package elevdatabase; import java.awt.*; import javax.swing.*; import com.borland.jbcl.layout.VerticalFlowLayout; import com.borland.jbcl.layout.BoxLayout2; import com.borland.jbcl.layout.OverlayLayout2; import java.awt.Toolkit.*; /** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */ public class PrintDialog extends JDialog { JPanel panel1 = new JPanel(); JRadioButton jRadioButton1 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); JRadioButton jRadioButton3 = new JRadioButton(); JRadioButton jRadioButton4 = new JRadioButton(); VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout(); GridBagLayout gridBagLayout1 = new GridBagLayout(); JButton jButton1 = new JButton(); public void centerDialog (Component owner, JDialog dialog) { dialog.setLocation(owner.getX() + (owner.getWidth() / 2) - (dialog.getWidth() / 2), owner.getY() + (owner.getHeight() / 2) - (dialog.getHeight() / 2)); } public PrintDialog(Frame owner, String title, boolean modal) { super(owner, title, modal); try { setDefaultCloseOperation(DISPOSE_ON_CLOSE); jbInit(); pack(); centerDialog (owner, this); } catch (Exception exception) { exception.printStackTrace(); } } public PrintDialog() { this(new Frame(), "PrintDialog", false); } private void jbInit() throws Exception { panel1.setLayout(verticalFlowLayout1); jRadioButton1.setText("jRadioButton4"); jRadioButton2.setText("jRadioButton3"); jRadioButton3.setText("Alle"); jRadioButton4.setText("jRadioButton2"); this.getContentPane().setLayout(gridBagLayout1); jButton1.setText("Print..."); panel1.setBorder(BorderFactory.createEtchedBorder()); panel1.add(jRadioButton1, null); panel1.add(jRadioButton2, null); panel1.add(jRadioButton3, null); panel1.add(jRadioButton4, null); this.getContentPane().add(jButton1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); this.getContentPane().add(panel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); } }
28. marts 2005 - 00:26
#8
Det er højst sandsynligt her der går galt: public PrintDialog() { this(new Frame(), "PrintDialog", false); } Når du tester - bruger du så denne constructor? Hvis ja, så vil din Frame jo altid ha' koordinaterne 0, 0 !
28. marts 2005 - 11:22
#9
Nu virker det. Jeg har gjort flg. Den kaldende kode ser nu sådan ud: public void jButton4_actionPerformed(ActionEvent e) { PrintDialog prnDialog = new PrintDialog(this, "PrintDialog", false); prnDialog.show(); } Og den contructor du angav er nu fjernet: public PrintDialog() { this(new Frame(), "PrintDialog", false); }
Kurser inden for grundlæggende programmering