我遇到问题,我实施了BorderLayout JPanel。如果我尝试在北区移动按钮作为示例,它将保留在默认位置。 这是我的代码:
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);
}
我想要例如我的" New"按钮位于窗口的左上角,写入" ip.setLocation(0,0);"
它默认保留在中心..
有什么想法吗?
答案 0 :(得分:3)
边框布局布置一个容器,安排并调整其大小 适合五个地区的组成部分:北,南,东,西,和 中心。
private void getData(Query ref){
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "onChildAdded");
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "onChildChanged");
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.i(TAG, "onChildRemoved");
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "onChildMoved");
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.i(TAG, "onCancelled");
}
});
}
北面板现在由以下两部分组成:
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);
(ip
)和JButton
(centerPanel
)包含其余组件。
答案 1 :(得分:1)
您要做的是使用AbsoluteLayout而不是BorderLayout,BorderLayout使用Cardinal方向在窗格上设置对象,例如North,East,South,West和Center 。您可能希望查看用于BorderLayout的JavaDoc。
例如,您需要将north
面板设置为BorderLayout()
north.setLayout(new BorderLayout());