我正在尝试自学如何使用Java swing和Window Builder Pro创建GUI,在观看了几个YouTube视频后,阅读了一些教程,我完成了以下内容。
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();
附加到每个按钮,并在文本区域显示该打印,但我不知道如何完成此操作。
我还想知道我创建的文本区域是否会导致打印文本延伸到可见框架之外。
答案 0 :(得分:4)
textpane.append("TEXTHERE");
将根据event
向您的文字区域添加任何所需文字。当我们append()
文本时,我们还可以在现有段落中添加新文本。如果您要清除JTextArea
,我建议您调用setText()
方法。
向JScrollPane
添加滚动条需要JTextArea
,可以通过以下方式完成:
import javax.swing.JScrollPane;
JScrollPane scrollPane = new JScrollPane(TEXTAREAHERE);
我个人喜欢将滚动窗格的属性设置为此,以确保JScrollBar只能向上和向下滚动并删除左右滚动:
scrollPane.setHorizonalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
答案 1 :(得分:2)