4

我只是在学习 Swing,并为自己构建了一个示例 GUI 作为练习。非常基本,它在JFrame. 在最后一个面板中,还有另一个面板和一个按钮。JPanel面板是我对,的扩展DrawPanelRectangle每次单击其下方的按钮重绘时,它都会随机绘制一个。这是我的代码:

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

public class Tester
{
    public static DrawPanel panelEastDrawPanelCenter;

    public static void main()
    {
        Tester gui = new Tester();
        gui.go();
    }
    public void go()
    {
        JFrame frame = new JFrame("This is the title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create all the Western Panel components
        JPanel panelWest = new JPanel();
        //    panelWest.setLayout(new BorderLayout());
        JButton panelWestButtonWest = new JButton("Western-most West Button");
        JButton panelWestButtonCenter = new JButton("Western-most Center Button");
        JButton panelWestButtonEast = new JButton("Western-most East Button");

        // Create all Center Panel components
        JPanel panelCenter = new JPanel();
            panelCenter.setBackground(Color.darkGray);
        JButton panelCenterButtonCenter = new JButton("Center Button");

        // Create all East Panel components
        JPanel panelEast = new JPanel();
        panelEastDrawPanelCenter = new DrawPanel();
            panelEastDrawPanelCenter.setSize(50,50);
        JButton panelEastButtonSouth = new JButton("Repaint");
            panelEastButtonSouth.addActionListener(new panelEastButtonSouthListener());

        // Add everything to the GUI
        // West Panel
        frame.getContentPane().add(BorderLayout.WEST, panelWest);
        panelWest.add(BorderLayout.WEST, panelWestButtonWest);
        panelWest.add(BorderLayout.CENTER, panelWestButtonCenter);     
        panelWest.add(BorderLayout.EAST, panelWestButtonEast);

        // Center Panel
        frame.getContentPane().add(BorderLayout.CENTER, panelCenter);
        panelCenter.add(BorderLayout.CENTER, panelCenterButtonCenter);

        // East Panel
        frame.getContentPane().add(BorderLayout.EAST, panelEast);
        panelEast.add(panelEastDrawPanelCenter);
        panelEast.add(panelEastButtonSouth);

        frame.pack();
        //frame.setSize(frame.getWidth(), 500);
        frame.setVisible(true);
    }
    class panelEastButtonSouthListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            panelEastDrawPanelCenter.repaint();
        }
    }
    class DrawPanel extends JPanel // JPanel that displays a rectangle upon clicking the button "Repaint"
    {
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.WHITE); // Removes previous rectangle
            g.fillRect(0,0, this.getWidth(),this.getHeight());

            g.setColor(randColor()); // Puts a new rectangle on screen, rand size and color
            int height = (int)(Math.random() * this.getHeight());
            int width = (int)(Math.random() * this.getHeight());
            int x = (int)(Math.random() * 20);
            int y = (int)(Math.random() * 20);
            g.fillRect(x,y, height,width);
        }
        public Color randColor()
        {
            int r = (int)(Math.random() * 255);
            int g = (int)(Math.random() * 255);
            int b = (int)(Math.random() * 255);
            return new Color(r, g, b);
        }
    }
}

我遇到的问题是,虽然我明确地setSize()DrawPanel对象(panelEastDrawPanelCenter)设置为50×50,但当我运行程序时,它(DrawPanel)仍然是按钮旁边的一个小面板,而panelEast(容器DrawPanel和按钮)仍然是相同的宽度(并且永远不会变宽)。我意识到我可以说

frame.pack();
frame.setSize(frame.getWidth(), 500);

如果我将东面板设置为使用BorderLayout或的布局BoxLayout,这将使DrawPanel显示更大。

  1. 但我不明白为什么设置DrawPanel对象的大小实际上并没有改变它的大小,为什么不管我做什么它都保持很小setSize(50,50);
  2. 我如何让中间面板停止调整这么大的大小,以便东面板可以调整为更宽?或者我如何调整东面板的大小?
4

3 回答 3

8

BorderLayout 尊重其西部和东部组件的首选宽度。并且它尊重其中心组件的首选宽度和高度。因此,您的 DrawPanel 应该覆盖getPreferredSize()并返回适当的首选尺寸。

由于它位于同样具有南组件的 BorderLayout 的中心,因此东面板的首选宽度将是绘图面板和按钮的首选宽度的最大值。

于 2012-06-19T07:59:30.900 回答
2

您是否尝试过使用panel.setPerferredSize(Dimension size);

于 2012-06-19T07:58:10.583 回答
2

尝试该setPreferredSize(Dimension)方法,setSize(int, int)然后调用 a pack()

于 2012-06-19T07:58:56.713 回答