2017-10-04 11 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; 
    } 

最初の行は可能私はそれが中心にされるように任意の長さ長さにエド。 2行目のボタンは中央に配置する必要があります。奇妙なことに、最後の行の最初のボタンの高さは他のボタンと異なります。彼らはすべて同じでなければなりません。

Resulting Dialog

答えて

0

1枚のパネルのコードで

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

もう一度ありがとうございます。私は今、正しい道を歩いている。 –

関連する問題