在观看了几个 youtube 视频并阅读了一些教程之后,我正在尝试自学如何使用 Java swing 和 Window Builder Pro 制作 GUI。
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JetstreamFrame extends JFrame {
private static final long serialVersionUID = 1L;
JTabbedPane tabPane;
JPanel buttonOnePanel;
JPanel buttonTwoPanel;
JPanel textDisplayPanel;
JTextPane textPane;
SpringLayout sl_textDisplayPanel;
SpringLayout springLayout;
SpringLayout sl_buttonTwoPanel;
SpringLayout sl_buttonOnePanel;
JButton ButtonTwo;
JButton ButtonOne;
public JetstreamFrame() {
springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
tabPane = new JTabbedPane(JTabbedPane.TOP);
setupTabPane();
buttonOnePanel = new JPanel();
sl_buttonOnePanel = new SpringLayout();
setupButtonOnePanel();
ButtonOne = new JButton("Click Here!");
setupButtonOne();
buttonTwoPanel = new JPanel();
sl_buttonTwoPanel = new SpringLayout();
setupButtonTwoPanel();
ButtonTwo = new JButton("Click Here!");
setupButtonTwo();
textDisplayPanel = new JPanel();
sl_textDisplayPanel = new SpringLayout();
setupTextDisplayPanel();
textPane = new JTextPane();
setupTextPane();
}
private void setupTabPane()
{
springLayout.putConstraint(SpringLayout.NORTH, tabPane, 0, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, tabPane, 0, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, tabPane, -198, SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, tabPane, 484, SpringLayout.WEST, getContentPane());
getContentPane().add(tabPane);
}
private void setupButtonOnePanel()
{
tabPane.addTab("Tab One", null, buttonOnePanel, null);
buttonOnePanel.setLayout(sl_buttonOnePanel);
}
private void setupButtonOne()
{
ButtonOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
}
});
sl_buttonOnePanel.putConstraint(SpringLayout.NORTH, ButtonOne, 99, SpringLayout.NORTH, buttonOnePanel);
sl_buttonOnePanel.putConstraint(SpringLayout.WEST, ButtonOne, 187, SpringLayout.WEST, buttonOnePanel);
buttonOnePanel.add(ButtonOne);
}
private void setupButtonTwoPanel()
{
tabPane.addTab("Tab Two", null, buttonTwoPanel, null);
buttonTwoPanel.setLayout(sl_buttonTwoPanel);
}
private void setupButtonTwo()
{
ButtonTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
}
});
sl_buttonTwoPanel.putConstraint(SpringLayout.NORTH, ButtonTwo, 99, SpringLayout.NORTH, buttonTwoPanel);
sl_buttonTwoPanel.putConstraint(SpringLayout.WEST, ButtonTwo, 187, SpringLayout.WEST, buttonTwoPanel);
buttonTwoPanel.add(ButtonTwo);
}
private void setupTextDisplayPanel()
{
springLayout.putConstraint(SpringLayout.NORTH, textDisplayPanel, 6, SpringLayout.SOUTH, tabPane);
springLayout.putConstraint(SpringLayout.WEST, textDisplayPanel, 0, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, textDisplayPanel, -10, SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, textDisplayPanel, 0, SpringLayout.EAST, getContentPane());
getContentPane().add(textDisplayPanel);
textDisplayPanel.setLayout(sl_textDisplayPanel);
}
private void setupTextPane()
{
sl_textDisplayPanel.putConstraint(SpringLayout.NORTH, textPane, 0, SpringLayout.NORTH, textDisplayPanel);
sl_textDisplayPanel.putConstraint(SpringLayout.WEST, textPane, 0, SpringLayout.WEST, textDisplayPanel);
sl_textDisplayPanel.putConstraint(SpringLayout.SOUTH, textPane, 182, SpringLayout.NORTH, textDisplayPanel);
sl_textDisplayPanel.putConstraint(SpringLayout.EAST, textPane, 0, SpringLayout.EAST, textDisplayPanel);
textDisplayPanel.add(textPane);
}
public void start()
{
this.setSize(500, 500);
this.setVisible(true);
}
}
这段代码的前提是创建一个带有多个选项卡的窗口,底部有一个文本区域,无论用户在哪个选项卡中都可见。我已经创建了文本区域、选项卡、按钮以及此 GUI 的按钮事件侦听器。
不幸的是,我读过的教程都没有向我展示如何将任何内容打印到文本区域。我想要一些类似的东西
System.out.println();
附加到每个按钮上,并在文本区域中显示该打印,但我不知道如何完成此操作。
我还想知道如果打印的文本超出可见框架,我创建的文本区域是否会在侧面产生滚动条。