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行目のボタンは中央に配置する必要があります。奇妙なことに、最後の行の最初のボタンの高さは他のボタンと異なります。彼らはすべて同じでなければなりません。
提案していただきありがとうございます。私はJDIalogを拡張し、複数のパネルを使用することを考えていませんでした。私はこれを勉強する時間を費やす必要があります。 –
さて、それもパネルですが、維持することは難しいです。私は例を追加しました。 –
もう一度ありがとうございます。私は今、正しい道を歩いている。 –