我正在尝试使用GridBagLayout创建JDialog,但是我无法以自己想要的方式进行操作。我是否使用了错误的布局模型?我的代码是:

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;
   }


第一行可以是任何长度,因此我希望将其基于长度居中。第二行上的按钮应居中。奇怪的是,最后一行中第一个按钮的高度不同于其他按钮。它们都应该相同。

java - JDialog GridBagLayout不产生我想要的外观-LMLPHP

最佳答案

尝试这个

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());
    }
}


和一个面板版本

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());
    }
}


在您的代码中,此按钮将调整最后一行上的第一个按钮的大小(x = 0)

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

07-24 09:33