沒有任何MCVE,它很難提供幫助,所以我做了一個簡單的示例應用程序,希望適合您應用程序。
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static MainFrame frame;
private MainFrame() {
this.setTitle("Focus on JTabbedPane");
this.setSize(new Dimension(800, 600));
this.getContentPane().add(initComponents());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel initComponents() {
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.add(new MyInfoPanel(), BorderLayout.NORTH);
panel.add(new MyTabPanel(), BorderLayout.CENTER);
return panel;
}
public static void createAndShowGui() {
frame = new MainFrame();
frame.setVisible(true);
}
}
僅與信息文本
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyInfoPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyInfoPanel() {
this.add(new JLabel("Some info text"));
}
}
的MyTabbedPanel
import javax.swing.JTabbedPane;
public class MyTabPanel extends JTabbedPane {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyTabPanel() {
this.addTab("Tab 1", new FirstTabPanel());
this.addTab("Tab 2", new SecondTabPanel());
}
}
兩個TabPanels
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FirstTabPanel extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton button;
JTextField tf;
public FirstTabPanel() {
tf = new JTextField("Textfield");
this.add(tf);
button = new JButton("Button");
button.addActionListener(this);
this.add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(button)) {
tf.setText("Button on was clicked!");
}
}
}
第一JPanel中SecondTabPanel
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SecondTabPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextArea area;
JTextField field;
public SecondTabPanel() {
this.setLayout(new BorderLayout(10, 10));
field = new JTextField();
this.add(field, BorderLayout.NORTH);
area = new JTextArea();
this.add(area, BorderLayout.CENTER);
field.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
area.setText(area.getText() + e.getKeyChar());
}
});
}
}
並啓動它
import javax.swing.SwingUtilities;
public class App {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame.createAndShowGui();
}
});
}
}
希望這個小程序會幫助你。
_「每個選項卡包含一個JPanel(具有空佈局)」_。不要使用空佈局。學習使用[LayoutManagers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) –
也可以獲得更好的幫助,發佈[最小,完整和可驗證的示例](http:/ /stackoverflow.com/help/mcve) –
謝謝peeskillet,這真的有幫助。沒有使用null佈局,它真的按我想要的方式工作,謝謝:) 是的,下次我肯定會得到更多信息的例子。 :) – Vineet