2013-06-04 104 views
1

我的代碼如下以及截圖。我試圖在JTextField上面獲得一個標籤。但是,當標籤顯示時,選項卡上的按鈕會消失。我的問題是如何獲得文本區域上方的標籤,但也顯示了按鈕?將JLabel添加到JTabbedPane中的JTextArea?

代碼標籤:

import java.awt.BorderLayout; 
import java.awt.ComponentOrientation; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JComboBox; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.JRadioButton; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 


public class TestApplication implements ActionListener { 

    public static void main(String[] args) { 
    final JFrame frame = new JFrame(); 
    frame.setSize(3000, 3000); 
    frame.setTitle("RBA Test Application"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 


    JLabel logLabel = new JLabel("Input/Output Log"); 


    JTextArea logbox = new JTextArea() { 
    @Override 
     public java.awt.Dimension getPreferredSize() { 
      return new Dimension(500, 500); 
     }; 
    }; 

    logLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
    logLabel.setText("Input/Output Log"); 

    logbox.add(logLabel); 



    JButton initialize = new JButton("Initialize"); 

    JButton connect = new JButton("Connect"); 

    JButton disconnect = new JButton("Disconnect"); 

    JButton shutdown = new JButton("Shut Down"); 

    JButton portsettings = new JButton("Port Settings"); 

    JButton online = new JButton("Go Online"); 

    JButton offline = new JButton("Go Offline"); 

    JButton status = new JButton("Status"); 

    JButton reboot = new JButton("Reboot"); 

    JButton account = new JButton("Account"); 

    JButton amount = new JButton("Amount"); 

    JButton reset = new JButton("Reset"); 

    JButton approvordecl = new JButton("Approve/Decline"); 

    JButton test = new JButton("Test Button #1"); 

    JButton testing = new JButton("Test Button #2"); 

    JRadioButton button = new JRadioButton("Radio Button"); 

    JRadioButton button2 = new JRadioButton("Radio Button"); 

    JCheckBox checkbox = new JCheckBox("Check Box"); 

    JCheckBox checkbox2 = new JCheckBox("Check Box"); 



    JPanel newButtonPanel = new JPanel() { 
     @Override 
      public java.awt.Dimension getPreferredSize() { 
       return new Dimension(1000, 50); 
      }; 
     }; 

    newButtonPanel.add(online); 
    newButtonPanel.add(offline); 
    newButtonPanel.add(status); 
    newButtonPanel.add(reboot); 
    newButtonPanel.add(account); 
    newButtonPanel.add(amount); 
    newButtonPanel.add(reset); 
    newButtonPanel.add(approvordecl); 


    JPanel testPanel = new JPanel(); 
    testPanel.add(button); 
    testPanel.add(button2); 
    testPanel.add(checkbox2); 

    JPanel posPanel = new JPanel(); 
    posPanel.add(test); 
    posPanel.add(testing); 
    posPanel.add(checkbox); 

    JPanel llpPanel = new JPanel(); 
    llpPanel.setLayout(new BorderLayout()); 
    llpPanel.add(newButtonPanel); 
    llpPanel.add(new JScrollPane(logbox), BorderLayout.PAGE_END); 
    llpPanel.add(logLabel); 

    JPanel buttonPanel = new JPanel(); 
    buttonPanel.add(initialize); 
    buttonPanel.add(connect); 
    buttonPanel.add(disconnect); 
    buttonPanel.add(shutdown); 
    buttonPanel.add(portsettings); 
    frame.add(buttonPanel); 
    frame.add(buttonPanel, BorderLayout.NORTH); 

    JTabbedPane tabbedPane = new JTabbedPane(); 
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol"); 
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale"); 
    tabbedPane.addTab("Test", null, testPanel, "Test"); 

    JPanel tabsPanel = new JPanel(new BorderLayout()); 
    tabsPanel.add(tabbedPane); 
    frame.add(tabsPanel, BorderLayout.CENTER); 


    frame.pack(); 



} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    // TODO Auto-generated method stub 

} 






} 

截圖與標籤:

label

代碼,而標籤:

import java.awt.BorderLayout; 
import java.awt.ComponentOrientation; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JComboBox; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.JRadioButton; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 


public class TestApplication implements ActionListener { 

     public static void main(String[] args) { 
    final JFrame frame = new JFrame(); 
    frame.setSize(3000, 3000); 
    frame.setTitle("RBA Test Application"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 


    JLabel logLabel = new JLabel("Input/Output Log"); 

    JTextArea logbox = new JTextArea() { 
    @Override 
     public java.awt.Dimension getPreferredSize() { 
      return new Dimension(500, 500); 
     }; 
    }; 

    logLabel.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N 
    logLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
    logLabel.setText("Input/Output Log"); 

    logbox.add(logLabel); 


    JButton initialize = new JButton("Initialize"); 

    JButton connect = new JButton("Connect"); 

    JButton disconnect = new JButton("Disconnect"); 

    JButton shutdown = new JButton("Shut Down"); 


    JButton portsettings = new JButton("Port Settings"); 

    JButton online = new JButton("Go Online"); 

    JButton offline = new JButton("Go Offline"); 

    JButton status = new JButton("Status"); 

    JButton reboot = new JButton("Reboot"); 


    JButton account = new JButton("Account"); 

    JButton amount = new JButton("Amount"); 

    JButton reset = new JButton("Reset"); 


    JButton approvordecl = new JButton("Approve/Decline"); 

    JButton test = new JButton("Test Button #1"); 

    JButton testing = new JButton("Test Button #2"); 

    JRadioButton button = new JRadioButton("Radio Button"); 

    JRadioButton button2 = new JRadioButton("Radio Button"); 

    JCheckBox checkbox = new JCheckBox("Check Box"); 

    JCheckBox checkbox2 = new JCheckBox("Check Box"); 



    JPanel newButtonPanel = new JPanel() { 
     @Override 
      public java.awt.Dimension getPreferredSize() { 
       return new Dimension(1000, 50); 
      }; 
     }; 

    newButtonPanel.add(online); 
    newButtonPanel.add(offline); 
    newButtonPanel.add(status); 
    newButtonPanel.add(reboot); 
    newButtonPanel.add(account); 
    newButtonPanel.add(amount); 
    newButtonPanel.add(reset); 
    newButtonPanel.add(approvordecl); 


    JPanel testPanel = new JPanel(); 
    testPanel.add(button); 
    testPanel.add(button2); 
    testPanel.add(checkbox2); 

    JPanel posPanel = new JPanel(); 
    posPanel.add(test); 
    posPanel.add(testing); 
    posPanel.add(checkbox); 

    JPanel llpPanel = new JPanel(); 
    llpPanel.setLayout(new BorderLayout()); 
    llpPanel.add(newButtonPanel); 
    llpPanel.add(new JScrollPane(logbox), BorderLayout.PAGE_END); 

    JPanel buttonPanel = new JPanel(); 
    buttonPanel.add(initialize); 
    buttonPanel.add(connect); 
    buttonPanel.add(disconnect); 
    buttonPanel.add(shutdown); 
    buttonPanel.add(portsettings); 
    frame.add(buttonPanel); 
    frame.add(buttonPanel, BorderLayout.NORTH); 

    JTabbedPane tabbedPane = new JTabbedPane(); 
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol"); 
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale"); 
    tabbedPane.addTab("Test", null, testPanel, "Test"); 

    JPanel tabsPanel = new JPanel(new BorderLayout()); 
    tabsPanel.add(tabbedPane); 
    frame.add(tabsPanel, BorderLayout.CENTER); 


    frame.pack(); 



} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    // TODO Auto-generated method stub 

} 






} 

截圖無標籤:

label

+0

確實標籤顯示在上面嗎? –

+0

等一下,你正在給JTextArea添加一個JLabel?它是否正確?如果正確,這是非常不尋常的,並不是真正的建議。此外,您正在設置太多東西的首選尺寸,以至於很容易看出這會導致組件彼此碰撞並互相覆蓋。 –

+0

是的,這是我想要做的。我是初學者,不知道什麼是平常的,什麼不是。我是一名實習生,我正在做一個測試應用程序,所以我的「幫手」,我想是你會稱之爲將幫助我清理我的代碼。你能告訴我我能做什麼嗎?我有點困惑,因爲另一個人說,它在Windows上顯示得很好。 –

回答

2

所有組件添加到llpPanel和順序,你會看到他們,從上到下:

llpPanel.add(newButtonPanel, BorderLayout.PAGE_START); 
llpPanel.add(logLabel, BorderLayout.CENTER); 
llpPanel.add(new JScrollPane(logbox), BorderLayout.PAGE_END); 
+0

工作!非常感謝!!! –