您好,在尝试构建我的Java GUI时,我一直在解决我的JPanel不断调整大小的问题,或者它们只是显示为小的折叠方块。我使用setSize()方法定义了每个JPanel的大小。
这就是我得到的
这更像是我正在努力建立的
这是我的代码。我想我的问题是如何阻止JPanels调整大小并坚持使用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();
}
}
}
}
答案 0 :(得分:4)
setSize(...)
。 getPreferredSize()
,但要谨慎使用非常。pack()
。例如,你可以......
答案 1 :(得分:0)
尝试使用另一个LayoutManager,例如GridLayout。