import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Created by joshuaogunnote on 31/10/2015.
*/
public class Applet2 extends JApplet {
JTextField value1, value2;
public void init() {
JLabel prompt = new JLabel("Please enter a word");
JLabel prompt1 = new JLabel("Please enter a letter");
value1 = new JTextField(3);
value2 = new JTextField(3);
setLayout(new FlowLayout());
add(prompt);
add(value1);
setLayout(new FlowLayout());
add(prompt1);
add(value2);
JButton but = new JButton("Add word");
JButton but1 = new JButton("Clear");
JButton but2 = new JButton("Remove first occurrence");
JButton but3 = new JButton("Remove all occurrences");
JButton but4 = new JButton("Display all words begging with certain letter");
JButton but5 = new JButton("Search");
JPanel butPanel = new JPanel();
JPanel butPanel1 = new JPanel();
JPanel butPanel2 = new JPanel();
butPanel.add(but);
butPanel.add(but1);
butPanel1.add(but2);
butPanel1.add(but3);
butPanel2.add(but4);
butPanel2.add(but5);
这是我试图确定Applet上按钮位置的地方。我正在使用BorderLayout.SOUTH尝试让按钮显示在applet的底部,但它不起作用。如何让按钮出现在Applet的底部?
add(butPanel, BorderLayout.SOUTH);
add(butPanel1, BorderLayout.SOUTH);
add(butPanel2, BorderLayout.SOUTH);
}
}
答案 0 :(得分:2)
您只能将一个组件添加到BorderLayout位置。因此,创建一个新的JPanel来按住按钮,向其添加按钮,并将其添加到南方位置。但请注意,BorderLayout常量仅在容器(此处为this
)使用BorderLayout时才有效!:
JButton but = new JButton("Add word");
JButton but1 = new JButton("Clear");
JButton but2 = new JButton("Remove first occurrence");
JButton but3 = new JButton("Remove all occurrences");
JButton but4 = new JButton("Display all words begging with certain letter");
JButton but5 = new JButton("Search");
JButton[] buttons = {but, but1, but2, but3, but4, but5};
JPanel bottomPanel = new JPanel();
for(JButton btn : buttons) {
bottomPanel.add(btn);
}
// note this will only work if the layout for *this* is in fact BorderLayout!
add(bottomPanel, BorderLayout.PAGE_END);
更完整的例子:
import java.awt.BorderLayout;
import javax.swing.*;
public class Foo extends JPanel {
private JTextField value1;
private JTextField value2;
public Foo() {
JLabel prompt = new JLabel("Please enter a word");
JLabel prompt1 = new JLabel("Please enter a letter");
value1 = new JTextField(3);
value2 = new JTextField(3);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.add(prompt);
topPanel.add(value1);
topPanel.add(prompt1);
topPanel.add(value2);
JButton but = new JButton("Add word");
JButton but1 = new JButton("Clear");
JButton but2 = new JButton("Remove first occurrence");
JButton but3 = new JButton("Remove all occurrences");
JButton but4 = new JButton(
"Display all words begging with certain letter");
JButton but5 = new JButton("Search");
JButton[] buttons = { but, but1, but2, but3, but4, but5 };
JPanel bottomPanel = new JPanel();
for (JButton btn : buttons) {
bottomPanel.add(btn);
}
add(topPanel, BorderLayout.PAGE_START);
add(new JScrollPane(new JTextArea(30, 40)), BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
Foo mainPanel = new Foo();
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
显示为:
答案 1 :(得分:2)
您将布局设置为FlowLayout,因此您无法使用BorderLayout的约束。不要这样做。 JApplet的默认布局是BorderLayout,因此无需更改布局。
此外,您只能将单个组件添加到BorderLayout的任何约束中。
因此,创建一个使用FlowLayout的面板。将按钮添加到面板然后将该面板添加到BorderLayout.SOUTH。
阅读How to Use BorderLayout上的Swing教程中有关工作示例的部分