-1

您好,在尝试构建我的 Java GUI 时,我一直遇到 JPanel 不断调整大小的问题,或者它们只是显示为折叠的小方块。我使用 setSize() 方法定义了每个 JPanel 的大小。

这就是我得到的

输出

这更多的是我想要构建的

小样

这是我的代码。我想我的问题是如何阻止 JPanel 调整大小并坚持由 setSize() 方法定义的宽度。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import javaQuery.j2ee.GeoLocation;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class iPerf extends JFrame implements ActionListener{

private static final int JFrame_W = 437;
private static final int JFrame_H = 586;
private static final int titlePanel_W = 437;
private static final int titlePanel_H = 46;
private static final int locationPanel_W = 437;
private static final int locationPanel_H = 46;
private static final int buttonPanel_W = 437;
private static final int buttonPanel_H = 47;
private static final int resultsPanel_W = 437;
private static final int resultsPanel_H = 444;
private static final int outputPanel_W = 371;
private static final int outputPanel_H = 172;
JPanel titlePanel = new JPanel();
JPanel locationPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JPanel resultsPanel = new JPanel();
JPanel pingResultsPanel = new JPanel();
JPanel iperfResultsPanel = new JPanel();

JLabel iperfTitle = new JLabel("iPerf");
JLabel cityTitleLabel =  new JLabel("City");
JLabel cityResultLabel = new JLabel("");
JLabel countryTitleLabel =  new JLabel("Country");
JLabel countryResultLabel = new JLabel("");
JLabel latitudeTitleLabel =  new JLabel("Latitude");
JLabel latitudeResultLabel = new JLabel("");
JLabel longitudeTitleLabel =  new JLabel("Longitude");
JLabel longitudeResultLabel = new JLabel("");

JScrollPane pingResultsScrollPane = new JScrollPane();
JScrollPane iPerfResultsScrollPane = new JScrollPane();

String results;
JTextArea label = new JTextArea(results);

JButton runButton = new JButton("Run iPerf");

public static void main(String[] args){
    iPerf w = new iPerf( );
    w.setVisible(true);
    w.setResizable(false);
    w.setExtendedState(JFrame.MAXIMIZED_BOTH);

}

public iPerf() {
    super();
    setSize(JFrame_W, JFrame_H);
    setTitle("iPerf"); 
    setLayout(new  FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Title Panel
    titlePanel.setSize(titlePanel_W, titlePanel_H);
    titlePanel.setBackground(Color.darkGray);
    titlePanel.add(iperfTitle);
    add(titlePanel);

    //Location Panel
    locationPanel.setSize(locationPanel_W, locationPanel_H);
    locationPanel.setBackground(Color.LIGHT_GRAY);
    locationPanel.setLayout(new GridLayout(2, 4));
    locationPanel.add(cityTitleLabel);
    locationPanel.add(countryTitleLabel);
    locationPanel.add(latitudeTitleLabel);
    locationPanel.add(longitudeTitleLabel);
    locationPanel.add(cityResultLabel);
    locationPanel.add(countryResultLabel);
    locationPanel.add(latitudeResultLabel);
    locationPanel.add(longitudeResultLabel);
    add(locationPanel);

    //Button Panel
    buttonPanel.setSize(buttonPanel_W, buttonPanel_H);
    buttonPanel.setBackground(Color.LIGHT_GRAY);
    buttonPanel.setLayout(new FlowLayout());
    runButton.addActionListener(this);
    buttonPanel.add(runButton);
    add(buttonPanel);

    //Results Panel
    resultsPanel.setSize(resultsPanel_W, resultsPanel_H);
    resultsPanel.setBackground(Color.darkGray);
    resultsPanel.setLayout(new FlowLayout());
    pingResultsPanel.setSize(outputPanel_W, outputPanel_H);
    pingResultsPanel.setBackground(Color.WHITE);
    pingResultsPanel.setLayout(new FlowLayout());
    iperfResultsPanel.setSize(outputPanel_W, outputPanel_H);
    iperfResultsPanel.setBackground(Color.WHITE);
    iperfResultsPanel.setLayout(new FlowLayout());
    iPerfResultsScrollPane.setViewportView(label);
    iperfResultsPanel.add(iPerfResultsScrollPane);

    resultsPanel.add(pingResultsPanel);
    resultsPanel.add(iperfResultsPanel);
    add(resultsPanel);

    }

public void actionPerformed(ActionEvent e) {
    String buttonString = e.getActionCommand();

    if (buttonString.equals("Run iPerf")) {
        System.out.println(buttonString + "it works!");

        runIperfEastTCP iperfEastTCPThread = new runIperfEastTCP();
        getLocation locationThread = new getLocation();
        runPing pingThread = new runPing();

        iperfEastTCPThread.start();
        locationThread.start();
        pingThread.start();
    }
}

private class runPing extends Thread {
    public void run() {
        try {
            String line;
            Process p = Runtime.getRuntime().exec("/sbin/ping -c 4 www.google.com");
            BufferedReader input = new BufferedReader(
                new InputStreamReader(p.getInputStream()));

            while ((line = input.readLine()) != null) {
                results += line + "\n";
                label.setText(results);
                System.out.println(line);
            }

            input.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

private class runIperfEastTCP extends Thread {
    public void run() {
        try {
            String line = "";
            Process p;

            p = Runtime.getRuntime().exec("/usr/local/bin/iperf -c 184.72.222.65 -p 5001 -w 64k -P 4 -i 1 -t 10 -f k");
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));

            while ((line = input.readLine()) != null) {
                results += line + "\n";
                label.setText(results);
                System.out.println(line);
            }

            input.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


private class getLocation extends Thread {
    public void run() {
        try {
            InetAddress thisIP = InetAddress.getLocalHost();

            GeoLocation _gl = new GeoLocation();

            _gl.GetGeoLocation(thisIP);
            String IP = _gl.IP;
            String Country = _gl.Country;
            String City = _gl.City;
            String Latitude = _gl.Latitude;
            String Longitude = _gl.Longitude;

            cityResultLabel.setText(City);
            countryResultLabel.setText(Country);
            latitudeResultLabel.setText(Latitude);
            longitudeResultLabel.setText(Longitude);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


}
4

2 回答 2

4
  1. 不要调用setSize(...)任何组件。
  2. getPreferredSize()如果您绝对需要设置大小,请考虑覆盖,但要非常谨慎地使用它。
  3. 您没有正确使用布局管理器。阅读布局管理器教程,因为它将解释并显示您需要知道的所有内容。
  4. 您需要pack()在将所有组件添加到 JFrame 之后并将其设置为可见或定位它之前调用您的 JFrame。

例如,您可以...

  • 考虑使用 BoxLayout 作为整体结构
  • 顶部可以使用文本居中的 JLabel。
  • JLabels 和 JButton 的下一部分可以使用 BorderLayout
  • BorderLayout 可以容纳在 BorderLayout.CENTER 位置包含四个标签/字段的 JPanel,
  • 同样的 JPanel 可以使用 GridBagLayout。
  • 持有 JButton 的 JPanel 可以使用 FlowLayout 并位于使用 JPanel 的 BorderLayout 的 BorderLayout.SOUTH 或 PAGE_END 位置。
  • 接下来 JPanel 可以使用带有边框和间隙的 GridLayout。
  • ETC...
于 2013-04-02T20:29:09.163 回答
0

尝试使用另一个 LayoutManager,例如 GridLayout。

于 2013-04-02T20:28:18.083 回答