【发布时间】:2019-03-22 16:49:18
【问题描述】:
我在查找特定问题的文档时遇到了一些麻烦。你看,我希望创建一组垂直对齐的按钮,它们粘在屏幕的最左侧,如这张编辑过的照片所示。
编辑屏幕以显示所需结果:
但是,我不知道如何实现这一点。 BoxLayout 的文档提到了 X 轴对齐,并且经过测试,它似乎专注于将组件彼此对齐,而不是组件与屏幕的相关部分对齐。
我的代码在这里:
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.*;
public class TabbedPanes extends JFrame implements ActionListener
{
JTabbedPane tabs;
JButton but1, but2, but3, but4, but5, but6, but7, but8, but9, but10, but11, but12;
String string1, string2;
JPanel pan1, pan2, pan3, pan4, pan5, pan6, pan7, pan8;
JFrame frame;
JScrollPane scroll;
public void Tabs()
{
Box theBox = Box.createVerticalBox();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but1 = new JButton("" + string1);
but1.addActionListener(this);
but3 = new JButton("" + string1);
but4 = new JButton("" + string1);
but5 = new JButton("" + string1);
but6 = new JButton("" + string1);
but7 = new JButton("" + string1);
but8 = new JButton("" + string1);
but9 = new JButton("" + string1);
but10 = new JButton("" + string1);
but11 = new JButton("" + string1);
but12 = new JButton("" + string1);
pan1 = new JPanel(); //instantiate the panel
pan1.add(theBox); //add the layout manager to the panel
theBox.add(but1); //add components to the layout manager
but1.setAlignmentX(Component.LEFT_ALIGNMENT);
theBox.add(but3);
theBox.add(but4);
theBox.add(but5);
theBox.add(but6);
theBox.add(but7);
theBox.add(but8);
theBox.add(but9);
theBox.add(but10);
theBox.add(but11);
theBox.add(but12);
JScrollPane scroll = new JScrollPane(pan1); //instantiate the JScrollPane with a parameter = the panel
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
pan2 = new JPanel();
but2 = new JButton("" + string2);
but2.addActionListener(this);
pan2.add(but2);
pan3 = new JPanel();
pan4 = new JPanel();
pan5 = new JPanel();
pan6 = new JPanel();
pan7 = new JPanel();
pan8 = new JPanel();
tabs = new JTabbedPane();
Container pane = frame.getContentPane();
pane.add(tabs);
tabs.add("Mon", scroll);
tabs.add("Tue", pan2);
tabs.add("Wed", pan3);
tabs.add("Thu", pan4);
tabs.add("Fri", pan5);
tabs.add("Sat", pan6);
tabs.add("Sun", pan7);
tabs.add("Notes", pan8);
frame.setSize(400,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == but1);
{
if(string1 == "Hello!")
{
string1 = "Goodbye.";
but1.setText(string1);
}
else
{
string1 = "Hello!";
but1.setText(string1);
}
}
if(e.getSource() == but2);
{
if(string2 == "Hello but in the other tab now!")
{
string2 = "I think you can see where this is going.";
but2.setText(string2);
}
else
{
string2 = "Hello but in the other tab now!";
but2.setText(string2);
}
}
}
public static void main(String[] args)
{
TabbedPanes TrueTabs = new TabbedPanes();
TrueTabs.Tabs();
}
}
它会产生这个屏幕:
代码当前生成的内容:
任何帮助将不胜感激!
【问题讨论】:
-
未来:1) 发布正确的minimal reproducible example。您的问题是关于按钮对齐的。您不需要 7 个选项卡来演示问题。而且你不需要 10 个按钮。我们需要查看的代码越少,就越容易发现问题。 2) 变量名不应以大写字符开头。大多数变量是正确的,但不是全部。始终如一。 3)不要使用“==”进行对象比较。使用
equals(...)方法。 -
将按钮向左对齐可以通过多种方式实现。最好考虑整个布局。你想对右边的空白区域做什么?
-
抱歉,我一直在尝试测试一大堆东西,却忘了清理我的代码。感谢您的帮助!
标签: java swing alignment layout-manager boxlayout