我試着用java和swing來做一些事情。我將不同的教程和示例代碼結合起來。我想在這裏做的是用選項卡構建一個jframe,每個選項卡將包含一個jpanel和一些jcomponents。第一個選項卡只有一個帶有BoxLayout中圖片的textarea,第二個和第三個選項卡上有一個包含某些項目的jlist,當它們被點擊時,它們會打開一些文件。當我運行代碼時,出現以下錯誤。任何幫助將非常感激。Swing BoxLayout
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(Unknown Source)
at javax.swing.BoxLayout.invalidateLayout(Unknown Source)
at javax.swing.BoxLayout.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at TabbedPaneDemo.createInnerText(TabbedPaneDemo.java:133)
at TabbedPaneDemo.<init>(TabbedPaneDemo.java:51)
at TabbedPaneDemo.main(TabbedPaneDemo.java:152)
+++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
JTabbedPane jtbExample = new JTabbedPane();
JPanel jplInnerPanel1 = createInnerText("Text");
jtbExample.addTab("General Info", null, jplInnerPanel1, "Tab 1");
jtbExample.setSelectedIndex(0);
JPanel jplInnerPanel2 = createInnerList();
jtbExample.addTab("Files", null, jplInnerPanel2, "Tab 2");
JPanel jplInnerPanel3 = createInnerList();
jtbExample.addTab("Files", null, jplInnerPanel3, "Tab 3");
// Add the tabbed pane to this panel.
setLayout(new GridLayout(1, 1));
add(jtbExample);
}
protected JPanel createInnerList() {
JPanel jplPanel = new JPanel(new BorderLayout());
JLabel label1 = new JLabel("Click to select File to Open");
jplPanel.add(label1, BorderLayout.PAGE_START);
String[] pdfstr = {"1st", "2nd"};
JList list = new JList(pdfstr);
JScrollPane scrollPane1 = new JScrollPane(list);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource();
if (evt.getClickCount() == 2) {
int index = list.locationToIndex(evt.getPoint());
if (index==1) {
try {
File pdfFile = new File(".pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File is not exists!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else if (evt.getClickCount() == 3) { // Triple-click
int index = list.locationToIndex(evt.getPoint());
}
}
});
// jplPanel.setLayout(new GridLayout(1, 1));
jplPanel.add(scrollPane1, BorderLayout.CENTER);
return jplPanel;
}
protected JPanel createInnerText(String text) {
Container jplPanel2 = new Container();
JPanel jplPanel = new JPanel(new BoxLayout(jplPanel2, BoxLayout.Y_AXIS));
JTextArea textarea = new JTextArea(200, 200);
textarea.setName("General Info");
textarea.setText(text);
textarea.setEditable(false);
textarea.setFont(new Font("Serif", Font.ITALIC, 14));
textarea.setForeground(Color.BLACK);
textarea.setBackground(new Color(0, 0, 0, 007));
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
JScrollPane areaScrollPane = new JScrollPane(textarea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(400, 400));
jplPanel.add(areaScrollPane);
try {
BufferedImage myPicture = ImageIO.read(new File(".jpg"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
jplPanel.add(picLabel);
} catch (IOException ex) {
System.out.println("Error");
}
return jplPanel;
}
public static void main(String[] args) throws HeadlessException, IOException {
JFrame frame = new JFrame("TabbedPane Source Demo") ;
frame.getContentPane().add(new TabbedPaneDemo(),
BorderLayout.CENTER);
frame.setResizable(false);
frame.setSize(800, 700);
frame.setVisible(true);
}
}