在 Swing 的垂直 BoxLayout 中将按钮粘贴到屏幕的最左侧
Gluing buttons to the far left of the screen in a vertical BoxLayout in Swing
我一直无法找到特定问题的文档。你看,我希望创建一组垂直对齐的按钮,粘在屏幕的最左侧,如这张编辑过的照片所示。
已编辑屏幕以显示所需结果:
但是,我不知道如何实现这一目标。 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();
}
}
它产生这个屏幕:
代码当前生成的内容:
如有任何帮助,我们将不胜感激!
您正在将 Box 添加到面板,该面板默认使用居中对齐的 FlowLayout。
所以你可以这样做:
//pan1 = new JPanel();
pan1 = new JPanel( new FlowLayout(FlowLayout.LEFT) );
或者您甚至不需要额外的面板。
只需将 Box 添加到滚动窗格:
//JScrollPane scroll = new JScrollPane(pan1);
JScrollPane scroll = new JScrollPane(theBox);
我一直无法找到特定问题的文档。你看,我希望创建一组垂直对齐的按钮,粘在屏幕的最左侧,如这张编辑过的照片所示。
已编辑屏幕以显示所需结果:
但是,我不知道如何实现这一目标。 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();
}
}
它产生这个屏幕:
代码当前生成的内容:
如有任何帮助,我们将不胜感激!
您正在将 Box 添加到面板,该面板默认使用居中对齐的 FlowLayout。
所以你可以这样做:
//pan1 = new JPanel();
pan1 = new JPanel( new FlowLayout(FlowLayout.LEFT) );
或者您甚至不需要额外的面板。
只需将 Box 添加到滚动窗格:
//JScrollPane scroll = new JScrollPane(pan1);
JScrollPane scroll = new JScrollPane(theBox);