如何将帧连接到现有帧?
以下代码是appletframe的代码。我想要做的是添加其他代码,该代码用于连接到AppletFrame底部的框架,这样当我拖动Appletframe时,我们也会拖动框架代码。基本上我希望框架代码与appletFrame连接,以便两个框架在一起。
appletFrame = new JFrame(Settings.serverName);
Loader.webclient = false;
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
appletPanel.setLayout(new BorderLayout());
appletFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/icon.png")));
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 537));
appletFrame.getContentPane().add(appletPanel, "Center");
appletFrame.pack();
appletFrame.setLocationRelativeTo(null);
appletFrame.setVisible(true);
JMenuBar jmenubar = new JMenuBar();
appletFrame.setJMenuBar(jmenubar);
Layout = new FlowLayout();
ImageIcon keyboard = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/keyboard.png")));
ImageIcon wrench = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/wrench.png")));
Button1 = new JButton("Vote");
Button2 = new JButton("Item List");
Button3 = new JButton("Screenshot");
Button4 = new JButton(wrench);
Button5 = new JButton(keyboard);
Button4.setBorder(null);
Button4.setBorderPainted(false);
Button4.setContentAreaFilled(false);
Button5.setBorder(null);
Button5.setBorderPainted(false);
Button5.setContentAreaFilled(false);
jmenubar.setLayout(Layout);
jmenubar.add(Button1);
jmenubar.add(Button2);
jmenubar.add(Button3);
jmenubar.add(Button4);
jmenubar.add(Button5);
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
Button4.addActionListener(this);
Button5.addActionListener(this);
Button1.setText("Vote");
Button2.setText("Item List");
Button3.setText("Screenshot");
我希望它与AppletFrame附加的框架。我希望将它附加到appletFrame的底部,但我不知道该怎么做。
JFrame frame = new JFrame();
frame.setSize(775,121);
frame.setResizable(false);
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
答案 0 :(得分:3)
正如我在第一篇评论中所提到的,这个GUI可以更好地组合到一个顶级容器中。
这是一个SSCCE 1 (在我的第二条评论中提到),它显示了基本的想法,虽然现在我对所需的效果有了更好的了解,JSplitPane
似乎不太合适。在这里,我只是将GUI元素组合到相同的布局中。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGUI extends JPanel {
TestGUI() {
JFrame appletFrame = new JFrame("Settings.serverName");
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
JPanel appletPanel = new JPanel(new BorderLayout());
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 537));
appletFrame.getContentPane().add(appletPanel, BorderLayout.CENTER);
// Don't use a menu-bar as a tool-bar!
JToolBar jmenubar = new JToolBar();
appletPanel.add(jmenubar, BorderLayout.PAGE_START);
JButton Button1 = new JButton("Vote");
JButton Button2 = new JButton("Item List");
JButton Button3 = new JButton("Screenshot");
JButton Button4 = new JButton("wrench");
JButton Button5 = new JButton("keyboard");
Button4.setBorder(null);
Button4.setBorderPainted(false);
Button4.setContentAreaFilled(false);
Button5.setBorder(null);
Button5.setBorderPainted(false);
Button5.setContentAreaFilled(false);
jmenubar.setLayout(new FlowLayout());
jmenubar.add(Button1);
jmenubar.add(Button2);
jmenubar.add(Button3);
jmenubar.add(Button4);
jmenubar.add(Button5);
JTextArea textArea = new JTextArea("TEST", 4, 65 );
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (
textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
appletPanel.add(scroll, BorderLayout.PAGE_END);
appletFrame.pack();
appletFrame.setLocationByPlatform(true);
appletFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
new TestGUI();
}
});
}
}