2017-10-04 18 views
1

GridBagLayout을 사용하여 JDialog를 만들려고하지만 원하는 모양을 얻을 수 없습니다. 아마도 잘못된 레이아웃 모델을 사용하고 있습니까? 내 코드는 다음과 같습니다JDialog GridBagLayout 원하는 모양을 생성하지 않습니다.

여기
import java.awt.Dialog; 
import java.awt.Dialog; 
import java.awt.GridBagLayout; 
import java.text.NumberFormat; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFormattedTextField; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class alertDialog { 

JDialog dialog=null; 
private GridBagLayout layout=new GridBagLayout(); 
private NumberFormat tempFormat = NumberFormat.getIntegerInstance(); 

public alertDialog(String type_,TimelineRecord timeLine_) { 

    dialog=new JDialog(); 
    dialog.setTitle("Alert"); 
    dialog.setSize(600, 200); 
    dialog.setLayout(layout); 
    dialog.setModalityType(Dialog.ModalityType.MODELESS); 
    dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
    Monitor mon=timeLine_.monitor; 
    int row=1; 
    JLabel msg=new JLabel(); 
    if (type_.equals("Temperature")) { 
     msg.setText(mon.getName()+" temperature has been reached"); 
    } 
    else { 
     msg.setText(type_); 
    } 
    JButton dismiss=new JButton("Dismiss"); 
    dismiss.addActionListener(new delayButton(timeLine_,dialog)); 
    JButton delay5=new JButton("Delay 5 Minutes"); 
    delay5.addActionListener(new delayButton(5,timeLine_,dialog)); 
    JButton delay10=new JButton("Delay 10 Minutes"); 
    delay10.addActionListener(new delayButton(10,timeLine_,dialog)); 
    JButton delay15=new JButton("Delay 15 Minutes"); 
    delay15.addActionListener(new delayButton(15,timeLine_,dialog)); 
    dialog.add(msg,makeGbc(0,row++)); 
    dialog.add(dismiss,makeGbc(1,row++)); 
    if (mon!=null) { 
     JLabel currentTemp=new JLabel("Current Temperature"); 
     JLabel newTarget=new JLabel("New Target"); 
     JFormattedTextField temp=new JFormattedTextField(tempFormat); 
     temp.setText(Double.toString(mon.current)); 
     JFormattedTextField target=new JFormattedTextField(tempFormat); 
     target.setText(Double.toString(mon.target)); 
     dialog.add(currentTemp,makeGbc(0,row)); 
     dialog.add(temp,makeGbc(1,row)); 
     dialog.add(newTarget,makeGbc(2,row)); 
     dialog.add(target,makeGbc(3,row++)); 
    } 
    dialog.add(delay5,makeGbc(0,row)); 
    dialog.add(delay10,makeGbc(1,row)); 
    dialog.add(delay15,makeGbc(2,row)); 
    dialog.setVisible(true); 
} 



public static void main(String[] args) { 
    TimelineRecord timeline=new TimelineRecord(); 
    new alertDialog("tod",timeline); 

} 
} 

가 TimeLineRecord입니다 :

import java.util.Timer; 

public class TimelineRecord { 
// text of event to take place (Saved to XML) 
public String eventText; 
// time of day for alarm (Saved to XML) 
public String tod; 
// target temp for alarm (Saved to XML) 
public double targetTemp; 
// pit temp to change to (Saved to XML) 
public double pitTemp; 
// meat type 
public String meat; 
// weight of meat 
public double weight; 
// Average cook time for this probe in minutes (Saved to XML) 
public long avgCookTime; 
// total cook time in minutes 
public long totalCookTime; 
// associated monitor instance (ID saved to XML) 
public Monitor monitor=null; 
// associated pit probe instance (ID saved to XML) 
public Monitor pit=null; 
// timer for alarm instance 
public Timer timer=null; 
// actual time of event 
public String actualTod; 
// actual temp at event 
public double actualTemp; 
// actual pit temp at event 
public double actualPitTemp; 
// Method to set variables 
public void setObject(String obj_,String value_) { 
    switch(obj_) { 
     case "eventText": eventText=value_; 
          break; 

     case "tod":   tod=value_; 
          break; 

     case "targetTemp": if (!value_.equals("")) { 
           targetTemp=Double.parseDouble(value_); 
          } 
          break; 

     case "pitTemp":  if (!value_.equals("")) { 
           pitTemp=Double.parseDouble(value_); 
          } 
          break; 

     case "meat":  meat=value_; 
          break; 

     case "weight":  if (!value_.equals("")) { 
           weight=Double.parseDouble(value_); 
          } 
          break; 

    } 
} 
} 

청취자 :

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JDialog; 

public class delayButton implements ActionListener { 

private int delay=0; 
private TimelineRecord timeLine=null; 
private JDialog dialog=null; 

public delayButton(int minutes_,TimelineRecord timeLine_,JDialog dialog_) { 
    delay=minutes_; 
    timeLine=timeLine_; 
    dialog=dialog_; 
} 

public delayButton(TimelineRecord timeLine_,JDialog dialog_) { 
    timeLine=timeLine_; 
    dialog=dialog_; 
} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    if (delay==0) { 
     System.out.println("Got dismiss for "+timeLine.monitor.getName()); 
    } 
    else { 
     System.out.println("Got "+Integer.toString(delay)+" minute delay for "+timeLine.monitor.getName()); 
    } 
    dialog.dispose();  
} 

} 

그리고 GBC 발전기 :

public static GridBagConstraints makeGbc(int x, int y) { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     Insets WEST_INSETS=new Insets(5,0,5,5); 
     Insets EAST_INSETS=new Insets(5,5,5,0); 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 

     gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
     gbc.fill = (x == 0) ? GridBagConstraints.BOTH 
      : GridBagConstraints.HORIZONTAL; 

     gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS; 
     gbc.weightx = (x == 0) ? 0.1 : 1.0; 
     gbc.weighty = 1.0; 
     return gbc; 
    } 

첫 번째 줄이 될 수 어떤 길이라도 나는 중심에 놓기를 바란다. 에드 길이. 두 번째 줄의 단추는 중앙에 위치해야합니다. 이상하게도 마지막 줄의 첫 번째 단추의 높이는 다른 단추와 다릅니다. 그들은 모두 같아야합니다.

Resulting Dialog

답변

0

이 마지막 행에서 첫 번째 버튼의 크기를 조절 코드에서 한 패널

import javax.swing.*; 
import java.awt.*; 

public class AlertDialog extends JDialog { 

    public AlertDialog(Frame owner) { 
     super(owner); 
     createGUI(); 
    } 

    public AlertDialog(Dialog owner) { 
     super(owner); 
     createGUI(); 
    } 

    private void createGUI() { 
     JLabel label = new JLabel("My Label"); 

     JButton button = new JButton("Button"); 

     JButton button1 = new JButton("Button 1"); 
     JButton button2 = new JButton("Button 2"); 
     JButton button3 = new JButton("Button 3"); 

     setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 
     setLayout(new GridBagLayout()); 

     GridBagConstraints c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.gridwidth = 3; 

     add(label, c); 

     c.gridy++; 
     c.weightx = 1.0; 
     c.weighty = 1.0; 

     add(button, c); 

     c.gridy++; 
     c.gridwidth = 1; 
     c.weighty = 0.0; 
     c.fill = GridBagConstraints.HORIZONTAL; 

     add(button1, c); 

     c.gridx++; 

     add(button2, c); 

     c.gridx++; 

     add(button3, c); 


     pack(); 
     setLocationRelativeTo(getParent()); 
    } 
} 

으로이

import javax.swing.*; 
import java.awt.*; 

public class AlertDialog extends JDialog { 

    public AlertDialog(Frame owner) { 
     super(owner); 
     createGUI(); 
    } 

    public AlertDialog(Dialog owner) { 
     super(owner); 
     createGUI(); 
    } 

    private void createGUI() { 
     JLabel label = new JLabel("My Label"); 

     JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     topPanel.add(label); 

     JButton button = new JButton("Button"); 

     JPanel centerPanel = new JPanel(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     centerPanel.add(button, c); 

     JButton button1 = new JButton("Button 1"); 
     JButton button2 = new JButton("Button 2"); 
     JButton button3 = new JButton("Button 3"); 

     JPanel bottomPanel = new JPanel(new GridLayout(0, 3)); 
     bottomPanel.add(button1); 
     bottomPanel.add(button2); 
     bottomPanel.add(button3); 

     setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 
     setLayout(new BorderLayout()); 

     add(topPanel, BorderLayout.PAGE_START); 
     add(centerPanel, BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 

     pack(); 
     setLocationRelativeTo(getParent()); 
    } 
} 

및 버전을 사용해 (X = 0)

gbc.fill = (x == 0) ? GridBagConstraints.BOTH 
     : GridBagConstraints.HORIZONTAL 
+0

제안 해 주셔서 감사합니다. 나는 JDIalog를 확장하고 여러 패널을 사용하는 것을 생각하지 않았습니다. 나는 이것을 공부하는 데 약간의 시간을 할애해야 할 것입니다. –

+0

음, 패널이기도하지만 유지 관리가 더 어렵습니다. 예제를 추가했습니다. –

+0

다시 한번 감사드립니다. 나는 지금 바른 길을 걷고있다. –