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);
这里是我试图确定小程序上按钮位置的地方。我正在使用 BorderLayout.SOUTH 尝试让按钮出现在小程序的底部,但它不起作用。如何让按钮出现在 Applet 的底部?
add(butPanel, BorderLayout.SOUTH);
add(butPanel1, BorderLayout.SOUTH);
add(butPanel2, BorderLayout.SOUTH);
}
}
最佳答案
您将布局设置为 FlowLayout,因此无法使用 BorderLayout 的约束。不要那样做。 JApplet的默认布局是BorderLayout,因此不需要更改布局。
此外,您只能将单个组件添加到 BorderLayout 的任何约束。
因此创建一个使用 FlowLayout 的面板。将按钮添加到面板然后将该面板添加到 BorderLayout.SOUTH。
阅读 Swing 教程中关于 How to Use BorderLayout 的部分工作示例
关于java - 如何将 JButton 放置在小程序的底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33459324/