我正在用java和swing尝试一些东西。我正在结合不同的教程和示例代码。我打算在这里做的是构建一个带有选项卡的 jframe,每个选项卡将包含一个带有一些 jcomponents 的 jpanel。第一个选项卡在 BoxLayout 中只有一个带有图片的文本区域,第二个和第三个选项卡有一个包含一些项目的 jlist,当它们被单击时,它们会打开一些文件。当我运行代码时,出现以下错误。任何帮助将非常感激。
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);
}
}