BorderLayout的,不可能设置了手动位置 - Java的SWING [英] BorderLayout, Impossible to set a manual Location - SWING Java
问题描述
我有一个问题,我实现了一个BorderLayout的JPanel的。如果我试图移动按钮,在北段为例,它会留在默认位置。
这里是我的code:
公共类窗口扩展的JFrame {
面板泛=新面板();
集装箱的JPanel,北,南,西,
公共JButton的IP,打印,取消,启动,OK;
JTextArea的时间步长;
JLabel的传奇;
双倍时间;
双温= 0.0;公共静态无效的主要(字串[] args){
新窗户();}公共窗口()
{
的System.out.println(猪乙脑LA);
this.setSize(700400);
this.setLocationRelativeTo(NULL);
this.setResizable(真);
this.setTitle(Assignment2 - CPU温度);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 集装箱=新JPanel(新的BorderLayout());
北=新JPanel();
IP =的新的JButton(新);
ip.set preferredSize(新尺寸(100,50));
ip.setLocation(0,0);
north.add(IP);
打印=的新的JButton(打印);
north.add(打印); north.add(新的JLabel(时间步长(在S):));
时间步长=新的JTextArea(10,1,5);
north.add(时间步);
开始=新的JButton(OK);
ListenForButton lForButton =新ListenForButton();
start.addActionListener(lForButton);
north.add(开始);
南=新JPanel();
传说=新的JLabel(传奇在这里);
south.add(传奇); 西=新JPanel();
JLabel的临时=新的JLabel(°C);
west.add(临时); container.add(北,BorderLayout.NORTH);
container.add(西BorderLayout.WEST);
container.add(锅,BorderLayout.CENTER);
container.add(南,BorderLayout.SOUTH); this.setContentPane(容器);
this.setVisible(真);
}
我要为我的例如新建按钮,以书面形式向被我的窗口左上角ip.setLocation(0,0);
和它保持在默认情况下该中心..
任何想法?
的甲骨文有关的BorderLayout文档
一个边界布局对容器,安排并调整其大小
组件符合下列五个区域:北,南,东,西,
中心
块引用>解决方案
北=新JPanel();
north.setLayout(新的BorderLayout());
IP =的新的JButton(新);
ip.set preferredSize(新尺寸(100,50));
打印=的新的JButton(打印);
north.add(IP,BorderLayout.WEST);JPanel的centerPanel =新JPanel();
centerPanel.add(打印);
centerPanel.add(新的JLabel(时间步长(在S):));
时间步长=新的JTextArea(10,1,5);
centerPanel.add(时间步);
开始=新的JButton(OK);
centerPanel.add(启动);north.add(centerPanel,BorderLayout.CENTER);北盘现由以下两部分组成:
IP
(的JButton
)和centerPanel
(的JPanel
),包含组件的其余部分。输出
I'm having a problem, i implemented a BorderLayout JPanel. And if i try to move a button for a example in the North section, it'll stay at a default location. Here's my code :
public class Window extends JFrame{ Panel pan = new Panel(); JPanel container, north,south, west; public JButton ip,print,cancel,start,ok; JTextArea timeStep; JLabel legend; double time; double temperature=0.0; public static void main(String[] args) { new Window(); } public Window() { System.out.println("je suis là"); this.setSize(700,400); this.setLocationRelativeTo(null); this.setResizable(true); this.setTitle("Assignment2 - CPU temperature"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container = new JPanel(new BorderLayout()); north = new JPanel(); ip = new JButton ("New"); ip.setPreferredSize(new Dimension(100,50)); ip.setLocation(0, 0); north.add(ip); print = new JButton ("Print"); north.add(print); north.add(new JLabel("Time Step (in s): ")); timeStep = new JTextArea("10",1,5); north.add(timeStep); start = new JButton("OK"); ListenForButton lForButton = new ListenForButton(); start.addActionListener(lForButton); north.add(start); south = new JPanel(); legend = new JLabel("Legends are here"); south.add(legend); west = new JPanel(); JLabel temp = new JLabel("°C"); west.add(temp); container.add(north, BorderLayout.NORTH); container.add(west,BorderLayout.WEST); container.add(pan, BorderLayout.CENTER); container.add(south, BorderLayout.SOUTH); this.setContentPane(container); this.setVisible(true); }
I want for example my "New" button to be at the left top corner of my window by writing "ip.setLocation(0,0);"
And it stays by default to the center..
Any ideas ?
解决方案Oracle Documentation about BorderLayout
A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center.
Solution
north = new JPanel(); north.setLayout(new BorderLayout()); ip = new JButton ("New"); ip.setPreferredSize(new Dimension(100,50)); print = new JButton ("Print"); north.add(ip, BorderLayout.WEST); JPanel centerPanel = new JPanel(); centerPanel.add(print); centerPanel.add(new JLabel("Time Step (in s): ")); timeStep = new JTextArea("10",1,5); centerPanel.add(timeStep); start = new JButton("OK"); centerPanel.add(start); north.add(centerPanel, BorderLayout.CENTER);
The north panel is now composed of the following two parts :
ip
(JButton
) andcenterPanel
(JPanel
) containing the rest of the components.
Output
这篇关于BorderLayout的,不可能设置了手动位置 - Java的SWING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!