如何创建3个相同大小的面板

时间:2015-12-02 17:43:57

标签: java swing

我在创建3个相同大小的面板时遇到问题,其中North面板是显示器,而南面板是显示器。中间面板分为东西两个部分,西部是文本框,用户可以输入字符串,东部是开始按钮。如何修复我的中心面板,使其与我的南北面板大小相同?

我的应用程序是什么样的:

enter image description here

正确的申请示例:

enter image description here

代码:

    import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ThreadedFillDemo2 extends JFrame implements ActionListener,
        Runnable {
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;
    public static final int FILL_WIDTH = 300;
    public static final int FILL_HEIGHT = 100;
    public static final int CIRCLE_SIZE = 10;
    public static final int PAUSE = 100; // milliseconds

    private JPanel northBox;
    private JPanel southBox;

    public static void main(String[] args) {
        ThreadedFillDemo2 gui = new ThreadedFillDemo2();
        gui.setVisible(true);
    }

    public ThreadedFillDemo2() {
        setSize(WIDTH, HEIGHT);
        setTitle("Location Finder");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new GridLayout(3,1));

        northBox = new JPanel();
        southBox = new JPanel();
        add(northBox);

        JTextField entry = new JTextField();
        JLabel entryTitle = new JLabel("Enter Postal Code"); 


        JButton startButton = new JButton("Get Location");
        startButton.addActionListener(this);



        JPanel buttonPanel = new JPanel();
        JPanel buttonPaneleast = new JPanel();
        JPanel buttonPanelwest = new JPanel();
        buttonPanel.setLayout(new GridLayout(1,2));
        buttonPanelwest.setLayout(new GridLayout(2,1));
        buttonPaneleast.setLayout(new GridLayout());

        buttonPanelwest.add(entryTitle);
        buttonPanelwest.add(entry);
        buttonPaneleast.add(startButton);



        buttonPanel.add(buttonPanelwest);
        buttonPanel.add(buttonPaneleast);
        add(buttonPanel);
        add(southBox);






    }

    public void actionPerformed(ActionEvent e) {
        startThread();
    }

    synchronized public void run() {
        Graphics g =northBox.getGraphics();
        for (int y = 0; y < FILL_HEIGHT; y = y + CIRCLE_SIZE)
            for (int x = 0; x < FILL_WIDTH; x = x + CIRCLE_SIZE) {
                g.fillOval(x, y, CIRCLE_SIZE, CIRCLE_SIZE);
                doNothing(PAUSE);
            }
    }

    synchronized public void startThread() {
        Thread theThread = new Thread(this);
        theThread.start();
    }

    synchronized public void doNothing(int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            System.out.println("Unexpected interrupt");
            System.exit(0);
        }
    }
}

0 个答案:

没有答案
相关问题