我在查找有关特定故障的文档时遇到了一些麻烦。您会看到,我希望创建一个垂直对齐的按钮组,该按钮组粘贴在屏幕的最左侧,如这张编辑的照片所示。
已编辑的屏幕可显示所需结果:
但是,我不知道如何实现这一目标。 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();
}
}
它会显示以下屏幕:
代码当前产生的内容:
任何帮助将不胜感激!
答案 0 :(得分:0)
您要将框添加到默认情况下使用中心对齐的FlowLayout的面板。
所以您可以这样做:
//pan1 = new JPanel();
pan1 = new JPanel( new FlowLayout(FlowLayout.LEFT) );
或者您甚至不需要其他面板。
只需将Box添加到滚动窗格中即可
//JScrollPane scroll = new JScrollPane(pan1);
JScrollPane scroll = new JScrollPane(theBox);