JButton 的对齐问题
这可能是在深夜发布的一个非常愚蠢的问题。我正在尝试创建两个 JButton,一个放在另一个之上。但由于某种原因,它没有正确对齐。底部按钮 b2 的左边缘稍微位于顶部底部 b1 左边缘的左侧。
这是代码:
class thistrial extends JPanel implements ActionListener
{
...
public thistrial()
{
.....
add(new JSeparator(SwingConstants.HORIZONTAL));
//ADD THE START AND STOP BUTTONS
Border raisedBorder = BorderFactory.createRaisedBevelBorder();
b1 = new JButton("START");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.CENTER);
b1.setPreferredSize(new Dimension(220,100));
add(new JSeparator(SwingConstants.HORIZONTAL));
b2 = new JButton("STOP");
b2.setPreferredSize(new Dimension(220,100));
b2.setVerticalTextPosition(AbstractButton.CENTER);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
add(b1);
add(b2);
.............
}
}
/** MAIN function **/
public static void main(String args[])
{
//Create and set up the window.
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.BLACK);
frame.setSize(398,480);
frame.setLocation(300,200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
thistrial newContentPane = new thistrial();
frame.setContentPane(newContentPane);
//Display the window.
frame.setVisible(true);
}
我能做什么呢?
This might be a very stupid question posted at the dead of the night. I am trying to create two JButtons placed one on top of the other. However for some reason, it is not getting properly aligned. The left edge of the bottom button b2 appears slightly to the left of the left edge of top bottom b1.
Here is the code:
class thistrial extends JPanel implements ActionListener
{
...
public thistrial()
{
.....
add(new JSeparator(SwingConstants.HORIZONTAL));
//ADD THE START AND STOP BUTTONS
Border raisedBorder = BorderFactory.createRaisedBevelBorder();
b1 = new JButton("START");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.CENTER);
b1.setPreferredSize(new Dimension(220,100));
add(new JSeparator(SwingConstants.HORIZONTAL));
b2 = new JButton("STOP");
b2.setPreferredSize(new Dimension(220,100));
b2.setVerticalTextPosition(AbstractButton.CENTER);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
add(b1);
add(b2);
.............
}
}
/** MAIN function **/
public static void main(String args[])
{
//Create and set up the window.
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.BLACK);
frame.setSize(398,480);
frame.setLocation(300,200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
thistrial newContentPane = new thistrial();
frame.setContentPane(newContentPane);
//Display the window.
frame.setVisible(true);
}
What can I do about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的示例扩展了
JPanel
,默认情况下使用FlowLayout
。相反,您可以尝试GridLayout
,如 所示布局管理器视觉指南。Your example extends
JPanel
, which usesFlowLayout
by default. Instead, you might tryGridLayout
, as shown in A Visual Guide to Layout Managers.我建议使用正确的布局管理器。你的代码没有显示任何内容。
I'd suggest using the right layout manager. Your code shows none.