package testerapplication;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
public class TesterApplication {
static JPanel CenterPanel;
static JPanel UpperPanel;
static JPanel CreationPanel;
static JPanel UpCreationPanel;
static JPanel DownCreationPanel;
static JPanel MenuPanel;
static JFrame frame;
JTextField RoadNameTextField;
JTextField BusNameTextField;
JTextField StopNameTextField;
TesterApplication(){
JFrame frame=new JFrame("Bus Map");
CenterPanel = new JPanel();
CenterPanel.setBackground(Color.black);
UpperPanel=new JPanel();
UpperPanel.setLayout(new BoxLayout(UpperPanel,BoxLayout.Y_AXIS));
MenuPanel=new JPanel();
MenuPanel.setBackground(Color.red);
CreationPanel=new JPanel();
CreationPanel.setBackground(Color.blue);
UpCreationPanel=new JPanel();
UpCreationPanel.setBackground(Color.blue);
DownCreationPanel=new JPanel();
DownCreationPanel.setBackground(Color.green);
//build text fields and put them into panel
BusNameTextField= new JTextField("Bus ",10);
StopNameTextField= new JTextField("Stope ",10);
RoadNameTextField= new JTextField("Street ",10);
UpCreationPanel.add(BusNameTextField);
UpCreationPanel.setLayout(new BoxLayout(UpCreationPanel,BoxLayout.X_AXIS));
DownCreationPanel.add(BorderLayout.WEST,StopNameTextField);
DownCreationPanel.add( RoadNameTextField);
CreationPanel.setLayout(new BoxLayout(CreationPanel,BoxLayout.Y_AXIS));
CreationPanel.add(UpCreationPanel);
CreationPanel.add(DownCreationPanel);
UpperPanel.add(MenuPanel);
UpperPanel.add(CreationPanel);
frame.getContentPane().add(BorderLayout.NORTH, UpperPanel);
frame.getContentPane().add(BorderLayout.CENTER,CenterPanel);
frame.setSize(600, 480);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args){
new TesterApplication();
}
}
[IMG] http://i60.tinypic.com/2yo3n21.png[/IMG]
问题是,或者更好的是。为什么BusNameTextField占满了窗口,为什么StopNameTextField没有到窗口的WEST-end,没有Blue UpCreationPanel,同时有Green DownCreationPanel是不是正常。
我的目的是让三个小TextField都放在左边,“Stope”和“Street”一个接一个
最佳答案
默认情况下,JPanel 使用 FlowLayout。
如果您希望组件垂直显示,那么您可以使用垂直 BoxLayout
或 GridBagLayout
。
并修复您的代码以遵循 Java 命名约定。变量名称不以大写字符开头。 “CenterPanel”应该是“centerPanel”等等。
最后不要使用静态变量。如果您对组件面板使用静态变量,则您的程序设计是错误的。
阅读 How to Use Layout Managers 上的 Swing 教程部分有关布局的更多信息以及如何构建程序的更好示例,包括在事件调度线程上创建 GUI。
关于java - 面板上文本字段的大小和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22406612/