Java 如何将JButtons放置在applet的底部?
这里是我试图确定小程序上按钮的位置的地方。我正在使用BorderLayout.SOUTH尝试让按钮显示在小程序的底部,但它不起作用。如何使按钮显示在小程序的底部Java 如何将JButtons放置在applet的底部?,java,swing,layout-manager,Java,Swing,Layout Manager,这里是我试图确定小程序上按钮的位置的地方。我正在使用BorderLayout.SOUTH尝试让按钮显示在小程序的底部,但它不起作用。如何使按钮显示在小程序的底部 import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * Created by joshuaogunnote on 31/10/2015. */ public class Applet2 extends JApplet { JText
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位置。因此,创建一个新的JPanel来保存按钮,将按钮添加到其中,并将其添加到南部位置。但请注意,只有当容器(此处
此
)使用BorderLayout!:
add(butPanel, BorderLayout.SOUTH);
add(butPanel1, BorderLayout.SOUTH);
add(butPanel2, BorderLayout.SOUTH);
}
}
一个更完整的示例:
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);
显示为:
只能将一个组件添加到BorderLayout位置。因此,创建一个新的JPanel来保存按钮,将按钮添加到其中,并将其添加到南部位置。但请注意,只有当容器(此处
此
)使用BorderLayout!:
add(butPanel, BorderLayout.SOUTH);
add(butPanel1, BorderLayout.SOUTH);
add(butPanel2, BorderLayout.SOUTH);
}
}
一个更完整的示例:
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);
显示为:
将布局设置为FlowLayout,因此不能使用BorderLayout的约束。不要那样做。JApplet的默认布局是BorderLayout,因此无需更改布局 此外,只能将单个组件添加到BorderLayout的任何约束中 因此,创建一个使用FlowLayout的面板。将按钮添加到面板,然后将该面板添加到BorderLayout.SOUTH
阅读Swing教程中关于工作示例的部分如果将布局设置为FlowLayout,则不能使用BorderLayout的约束。不要那样做。JApplet的默认布局是BorderLayout,因此无需更改布局 此外,只能将单个组件添加到BorderLayout的任何约束中 因此,创建一个使用FlowLayout的面板。将按钮添加到面板,然后将该面板添加到BorderLayout.SOUTH
阅读Swing教程中关于工作示例的部分为什么要编写小程序?如果是老师指定的,请参考。为什么要编写小程序?如果是老师指定的,请参考。