问题描述
界面:
错误:
我正在制作一个小程序来编写我的工作。我已经遇到这个错误有一段时间了,无法解决它。
每次我按下按钮来制作 JInternalFrame
时,面板的数量都会加倍,并在我尝试调整内部框架大小时显示。
我怀疑我没有正确使用 JSplitPane
和内部框架,但我不确定问题出在哪里。
这是我的完整代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudyNotesGUI extends JFrame{
static JFrame f = new JFrame();
JPanel panelmain = new JPanel();
JPanel panelleft = new JPanel();
JPanel panelfordoc = new JPanel();
JPanel panelright = new JPanel();
JPanel TextEditors = new JPanel();
JPanel UpperToolsMenu = new JPanel();
JPanel LowerToolsMenu = new JPanel();
final JDesktopPane desktop = new JDesktopPane();
JTextArea document = new JTextArea();
JButton[] UTButtons = new JButton[2];
JButton[] LTButtons = new JButton[4];
JComboBox fontBox;
static JPanel panelCalc = new JPanel();
static JPanel panelWB = new JPanel();
JInternalFrame web_browser = new JInternalFrame("Web browser",true,false);
JInternalFrame calculator = new JInternalFrame("Calculator",false);
public StudyNotesGUI() {
//Frame characteristics
f.setTitle("Study Notes v 0.00.1");
f.setLayout(new GridLayout());
// document characteristics
document.setLineWrap(true);
document.setWrapStyleWord(true);
document.setBorder(BorderFactory.createraisedSoftBevelBorder());
//scroll pane
JScrollPane scrollPane = new JScrollPane(document);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
// Main panel layouts
//f.setLayout(new BorderLayout());
panelmain.setLayout(new BorderLayout());
panelleft.setLayout(new BorderLayout());
panelright.setLayout(new BorderLayout());
JSplitPane splitPane = new JSplitPane();
splitPane.setBounds(1,1,1);
// Additional panel layouts
panelfordoc.setLayout(new GridLayout());
TextEditors.setLayout(new FlowLayout());
UpperToolsMenu.setLayout(new GridLayout());
LowerToolsMenu.setLayout(new GridLayout());
//MetaMenu.setLayout(new GridLayout());
// Document settings
document.setFont(new Font("Arial",Font.PLAIN,20));
//Menu bar
JMenuBar menuBar = new JMenuBar();
JMenu saveMenu = new JMenu("Save");
JMenu loadMenu = new JMenu("Load");
menuBar.add(saveMenu);
menuBar.add(loadMenu);
//Buttons
// Text editor buttons
// Font editor
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
JComboBox font = new JComboBox(fonts);
//font.setEditable(true);
font.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
document.setFont(new Font((String) font.getSelectedItem(),document.getFont().getSize()));
}
});
TextEditors.add(font);
JButton bold = new JButton("B");
TextEditors.add(bold);
JButton italic = new JButton("I");
TextEditors.add(italic);
JButton underline = new JButton("U");
TextEditors.add(underline);
JButton underline2 = new JButton("U2");
TextEditors.add(underline2);
JButton upper = new JButton("UP");
TextEditors.add(upper);
JButton lower = new JButton("LO");
TextEditors.add(lower);
JButton paragraph = new JButton("Par");
TextEditors.add(paragraph);
// Upper tools for the free space buttons
UTButtons[0] = new JButton("");
UpperToolsMenu.add(UTButtons[0]);
UTButtons[0].addActionListener(e -> {
//web_browser.putClientProperty("JInternalFrame.isPalette",Boolean.TRUE);
//web_browser.getRootPane().setwindowdecorationStyle(JRootPane.NONE);
web_browser.setVisible(true);
panelWB.setBackground(Color.GREEN);
//new SNGUINote();
web_browser.add(panelWB);
web_browser.setMinimumSize(new Dimension(200,200));
web_browser.setPreferredSize(new Dimension(600,400));
web_browser.pack();
desktop.add(web_browser);
});
UTButtons[1] = new JButton("Calculator");
UpperToolsMenu.add(UTButtons[1]);
UTButtons[1].addActionListener(e -> {
//calculator.putClientProperty("JInternalFrame.isPalette",Boolean.TRUE);
//calculator.getRootPane().setwindowdecorationStyle(JRootPane.NONE);
new SNGUICal();
});
for (int i = 0; i < 2 ; i++) {
UTButtons[i].setFocusable(false);
}
// Lower tools for the free space buttons
LTButtons[0] = new JButton("FastFind");
LowerToolsMenu.add(LTButtons[0]);
LTButtons[1] = new JButton("Draw");
LowerToolsMenu.add(LTButtons[1]);
LTButtons[2] = new JButton("Analytics");
LowerToolsMenu.add(LTButtons[2]);
LTButtons[3] = new JButton("Schedule");
LowerToolsMenu.add(LTButtons[3]);
for (int i = 0; i < 4 ; i++) {
LTButtons[i].setFocusable(false);
}
desktop.setBackground(Color.gray);
// // // // // // // // //
desktop.add(calculator);
//scrollPane.add(document);
panelleft.add(scrollPane,BorderLayout.CENTER);
//panelleft.add(document);
panelleft.add(TextEditors,BorderLayout.norTH);
panelright.add(UpperToolsMenu,BorderLayout.norTH);
panelright.add(LowerToolsMenu,BorderLayout.soUTH);
panelright.add(desktop,BorderLayout.CENTER);
splitPane.setLeftComponent(panelleft);
splitPane.setRightComponent(panelright);
panelmain.add(splitPane);
f.add(panelmain);
f.setJMenuBar(menuBar);
splitPane.setResizeWeight(0.4);
document.setPreferredSize(new Dimension(450,450));
calculator.pack();
}
/* public void actionPerformed(ActionEvent e) {
if (e.getSource() == UTButtons[0]) {
}
if (e.getSource() == UTButtons[1]) {
}
} */
public static void main(String[] args) {
new StudyNotesGUI();
f.setDefaultCloSEOperation(WindowConstants.EXIT_ON_CLOSE);
f.setResizable(true);
f.setVisible(true);
f.pack();
f.setLocationRelativeto(null);
f.setSize(1440,900);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)