从JFrame获取新的绑定值(Java)

时间:2014-04-16 08:43:06

标签: java swing jframe

我使用以下代码来测试我的JFrame的绑定值:

Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
r1 = this.getBounds();
this.setExtendedState(JFrame.MAXIMIZED_BOTH); //this == JFrame
r2 = this.getBounds();

首先,我认为r2的值(关于x,y,宽度和高度)会有所不同,因为帧会自动调整大小。但是,根据调试器,变量r1和r2似乎(几乎)相等。

为什么会这样?我应该怎么做才能获得最大化JFrame的新值?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class NewGUI extends JFrame {

    private JPanel contentPane;
    private List list;
    private JButton btnSelectAgent;
    private JTextField textField;
    private JButton btnBrowse;
    private JButton btnPreviousStep2;
    private JButton btnLoad;
    private JButton btnPreviousStep3;
    private JButton btnStart;
    private File file = null;
    ImageIcon icon = new ImageIcon("image.gif");
    Border blackline = BorderFactory.createLineBorder(Color.black);
    private JPanel firstStepPanel;
    private JPanel secondStepPanel;
    private JPanel thirdStepPanel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NewGUI frame = new NewGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public NewGUI() {
        super("GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 681, 422);
        contentPane = new JPanel();
        contentPane.setForeground(Color.BLACK);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        setLocationRelativeTo(null); //centered location
        contentPane.setLayout(null);

        //test codes begin
        Insets insets = this.getInsets();
        Rectangle r = new Rectangle();
        Dimension d = new Dimension();
        r.x = 0;
        r.y = 0;
        r.width = 1280;
        r.height = 1024;
        r = this.getMaximizedBounds();
        d = this.getMaximumSize();
        Rectangle r1 = new Rectangle();
        Rectangle r2 = new Rectangle();
        Dimension d1 = new Dimension();
        Dimension d2 = new Dimension();
        d1 = this.getSize();
        r1 = this.getBounds();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        r = this.getMaximizedBounds();
        r2 = r1;
        this.setMaximizedBounds(r);
        r2.width = this.getWidth();
        r2.height = this.getHeight();
        d2 = this.getSize();

        //test codes end
        /*        pane.setLayout(null); 
         JButton b1 = new JButton("one"); 
         JButton b2 = new JButton("two"); 
         JButton b3 = new JButton("three"); 
         pane.add(b1); 
         pane.add(b2); 
         pane.add(b3); 
         JPanel pane = new JPanel();

         Insets insets = this.getInsets();
         Rectangle r = new Rectangle();
         r.x = insets.left;
         r.y = insets.top;



         JFrame frame = new JFrame();
         frame.getInsets();
         Dimension size = b1.getPreferredSize();
         b1.setBounds(25 + insets.left, 5 + insets.top, size.width, size.height);
         Rectangle r = new Rectangle();
         r.x = 1;
         r.y = 2;
         b1.setBounds
         size = b2.getPreferredSize();
         b2.setBounds(55 + insets.left, 40 + insets.top, size.width, size.height); 
         size = b3.getPreferredSize(); 
         b3.setBounds(150 + insets.left, 15 + insets.top, size.width + 50, size.height + 20);
         ...//In the main method: 
         Insets insets = frame.getInsets(); 
         frame.setSize(300 + insets.left + insets.right, 125 + insets.top + insets.bottom);        

         */
        firstStepPanel = new JPanel();
        firstStepPanel.setBounds(10, 10, 194, 363);
        contentPane.add(firstStepPanel);
        //this.add(firstStepPanel, BorderLayout.WEST);
        firstStepPanel.setLayout(null);
        firstStepPanel.setBorder(
                BorderFactory.createTitledBorder(blackline, "Agent selection"));

        //makeFrameFullSize(contentPane); //aparentemente nao funcionando
        list = new List();
        list.setBounds(10, 31, 169, 203);
        firstStepPanel.add(list);
        list.setMultipleSelections(true);

        btnSelectAgent = new JButton("Select Agent");
        btnSelectAgent.setBounds(10, 274, 169, 34);
        firstStepPanel.add(btnSelectAgent);

        secondStepPanel = new JPanel();
        secondStepPanel.setBorder(
                BorderFactory.createTitledBorder(blackline, "file selection"));
        secondStepPanel.setBounds(214, 11, 266, 362);
        contentPane.add(secondStepPanel);
        //this.add(secondStepPanel, BorderLayout.CENTER);
        secondStepPanel.setLayout(null);

        btnLoad = new JButton("Load");
        btnLoad.setBounds(78, 317, 118, 34);
        secondStepPanel.add(btnLoad);
        btnLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (file == null) {
                    showMessage("Please, select a file on the upper options");
                } else {
                    disableSecondStep();
                    enableThirdStep();
                }

            }
        });
        btnLoad.setEnabled(false);

        textField = new JTextField();
        textField.setBounds(10, 29, 157, 34);
        secondStepPanel.add(textField);
        textField.setEnabled(false);
        textField.setColumns(10);
        textField.setText("P:\\\\");

        btnBrowse = new JButton("Browse");
        btnBrowse.setBounds(167, 29, 89, 35);
        secondStepPanel.add(btnBrowse);
        btnBrowse.setEnabled(false);

        btnPreviousStep2 = new JButton("<< Previous");
        btnPreviousStep2.setBounds(78, 272, 118, 34);
        secondStepPanel.add(btnPreviousStep2);
        btnPreviousStep2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableFirstStep();
                disableSecondStep();
            }
        });
        btnPreviousStep2.setEnabled(false);

        thirdStepPanel = new JPanel();
        thirdStepPanel.setBorder(
                BorderFactory.createTitledBorder(blackline, "Process initialization"));
        thirdStepPanel.setBounds(490, 10, 165, 362);
        contentPane.add(thirdStepPanel);
        //this.add(thirdStepPanel, BorderLayout.EAST);
        thirdStepPanel.setLayout(null);
        //thirdStepPanel.setSize(100, r.height);
        //r.x += 100;
        //thirdStepPanel.setBounds(r);
        //thirdStepPanel.setBounds(r.x,r.y, r.width, r.height);
        //thirdStepPanel.setAlignmentX(r.x);
        //thirdStepPanel.setAlignmentY(r.y);

        btnPreviousStep3 = new JButton("<< Previous");
        btnPreviousStep3.setBounds(7, 56, 151, 34);
        thirdStepPanel.add(btnPreviousStep3);
        btnPreviousStep3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableSecondStep();
                disableThirdStep();
            }
        });
        btnPreviousStep3.setEnabled(false);

        btnStart = new JButton(icon);
        btnStart.setBounds(7, 101, 151, 159);
        thirdStepPanel.add(btnStart);
        btnStart.setEnabled(false);
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showMessage("Process started!");
            }
        });
        //btnStart.setBackground(Color.RED);
        btnStart.setForeground(Color.BLACK);
        btnStart.setOpaque(false);
        btnStart.setBorderPainted(false);
        btnStart.setContentAreaFilled(false);
        btnBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser("some folder blablabla");
                int returnVal = fc.showOpenDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    file = fc.getSelectedFile();
                    //System.out.println(file.getPath());
                    textField.setText(file.getPath());
                } else if (returnVal == JFileChooser.CANCEL_OPTION) {

                    //System.out.println("File dialog cancelled");
                } else if (returnVal == JFileChooser.ERROR_OPTION) {

                    showMessage("Error in the file dialog!");
                }
            }
        });
        btnSelectAgent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (list.getSelectedItems().length == 0) {
                    showMessage("Please, select an Agent in the Agent's list");
                } else {
                    disableFirstStep();
                    enableSecondStep();

                }
            }
        });
        list.add("Zemax");
        list.add("Roundspot");
        list.add("CCDCamera");
        list.add("HexapodXYTable");

        /*        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
         this.add(firstStepPanel, BorderLayout.WEST);
         this.add(secondStepPanel, BorderLayout.CENTER);
         this.add(thirdStepPanel, BorderLayout.EAST);*/
    }

    private void showMessage(String msg) {
        JOptionPane.showMessageDialog(this, msg);
    }

    private void enableFirstStep() {
        list.setEnabled(true);
        btnSelectAgent.setEnabled(true);
    }

    private void enableSecondStep() {
        textField.setEnabled(true);
        btnBrowse.setEnabled(true);
        btnPreviousStep2.setEnabled(true);
        btnLoad.setEnabled(true);

    }

    private void enableThirdStep() {
        btnPreviousStep3.setEnabled(true);
        btnStart.setEnabled(true);

    }

    private void disableFirstStep() {
        list.setEnabled(false);
        btnSelectAgent.setEnabled(false);
    }

    private void disableSecondStep() {
        textField.setEnabled(false);
        btnBrowse.setEnabled(false);
        btnPreviousStep2.setEnabled(false);
        btnLoad.setEnabled(false);
    }

    private void disableThirdStep() {
        btnPreviousStep3.setEnabled(false);
        btnStart.setEnabled(false);
    }

    private void makeFrameFullSize(JPanel panel) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        panel.setSize(screenSize.width, screenSize.height);
    }

    BufferedImage image;

    public void nextButton() {
        try {
            image = ImageIO.read(new File("C:\\Users\\BAYGONCALVE\\Desktop\\red_button.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.drawImage(image, 0, 0, null);
    }

    public Dimension getPreferredSize() {
        return new Dimension(image.getWidth(), image.getHeight());
    }
}

2 个答案:

答案 0 :(得分:1)

主要问题是,框架实际上是不可见的。

例如,在我清理了你的代码后,我得到了......

r1 = java.awt.Rectangle[x=1280,y=820,width=0,height=0]
r2 = java.awt.Rectangle[x=1280,y=820,width=0,height=0]
d1 = java.awt.Dimension[width=0,height=0]
d2 = java.awt.Dimension[width=0,height=0]

(取消了对setBounds的呼吁以及其他一些事情)。基于窗口尚未实现的事实(未示出),对于我来说,尺寸实际上没有变化并不奇怪,因为直到你显示窗口,它没有将显示GraphicsDevice的上下文,因此无法确定实际最大大小...

如果你在构造函数中调用setVisible(true),那么在调用setExtendedState(JFrame.MAXIMIZED_BOTH)之前我会...

r1 = java.awt.Rectangle[x=1214,y=801,width=132,height=38]
r2 = java.awt.Rectangle[x=-8,y=32,width=2576,height=1576]
d1 = java.awt.Dimension[width=132,height=38]
d2 = java.awt.Dimension[width=2576,height=1576]

旁注......

  • 不要使用null布局。像素完美布局是现代UI设计中的一种幻觉,您无法控制字体,DPI,渲染管道或其他因素,这些因素将改变组件在屏幕上呈现的方式。 Swing旨在与布局管理器一起克服这些问题。如果你坚持忽略这些功能并违背API设计,那就要为很多头痛做好准备,永远不要过时努力......
  • 不要混合重量轻和重量轻的组件,AWT和Swing组件不能很好地协同工作,而不是使用java.awt.List尝试使用javax.swing.JList代替

答案 1 :(得分:0)

试试这个

frame.getWidth() and frame.getHeight()

获取没有标题和其他边框的JFrame的大小

frame.getContentPane().getSize();