我正在嘗試自學如何使用Java swing和Window Builder Pro製作GUI,看完幾個YouTube視頻後,閱讀我已完成以下內容的一些教程。第一次嘗試在一個簡單的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();
行的東西,並具有打印文本區域顯示出來,但我不知道如何做到這一點。
我還想知道,如果我創建的文本區域的結果是沿着側面的滾動條,那麼打印的文本應該超出可見框架。
*「使用Java swing和Window Builder Pro,觀看了幾個YouTube視頻後..」*(不寒而慄)前進的道路很長。將此鏈接加入收藏夾 - [使用JFC/Swing創建GUI](http://docs.oracle.com/javase/tutorial/uiswing/)。許多即將到來的問題最終都會以鏈接回到部分。 –