【发布时间】:2017-12-11 22:55:30
【问题描述】:
我有一个简单的 java 程序,当我使用 eclipse 运行它时,它会显示我为布局设置的 3 个 JButton。按钮设置为更改布局的对齐方式。所以你按左对齐左对齐右对齐,居中对齐居中。
当按钮执行此操作时,窗口中的对齐方式不会改变,直到您调整它的大小。
我尝试更新 jdk 和 eclipse 并没有产生任何影响,我看不出代码本身有问题。
有人知道这是为什么吗?
导入 javax.swing.JFrame;
public class Main {
public static void main(String []args){
Layout_buttonsAndActionEvents layout = new Layout_buttonsAndActionEvents();
layout.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
layout.setSize(300,300);
layout.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Layout_buttonsAndActionEvents extends JFrame {
private static final long serialVersionUID = 1L;
private JButton leftButton;
private JButton rightButton;
private JButton centerButton;
private FlowLayout layout;
private Container container;
public Layout_buttonsAndActionEvents(){
super("The Title");
layout = new FlowLayout();
container = new Container();
setLayout(layout);
leftButton = new JButton("Left");
add(leftButton);
//Align to the left
leftButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(container);
}
});
centerButton = new JButton("Center");
add(centerButton);
//Align to the right
centerButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(container);
}
});
rightButton = new JButton("Right");
add(rightButton);
//Align to the right
rightButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.RIGHT);
layout.layoutContainer(container);
}
});
}
}
【问题讨论】:
-
更改对齐后是否重新打包窗口?
-
你能发布你的代码吗?
-
请不在 cmets 中。您可以编辑您的问题并将其添加到那里。
-
抱歉第一次在堆栈上问任何东西