我正在尝试使用BorderLayout将一些元素组合到JPanel中。 我在中心有一个JSlider,如果调用该构造函数,则在西部有一个Name。然后在东边我有另一个用于控制滑块的面板,这个面板提供了所有问题。
在东面板上,我想要一个JTextField和两个按钮堆叠。我在中心有一个JTextField,然后在东面有一个子面板,它在北/南保持按钮,使它们堆叠。
当我运行程序时,东面板缺少JTextField,按钮的行为分别设置为西/东,而不是北/南。
以下是我编写并查看了好几个小时的代码:
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
/*
Made by vzybilly
Look of class:
____________________________________________________
| | | | [+] |
| <Name, if called>: | <Slider> | <Text box> | |
| | | | [-] |
----------------------------------------------------
*/
public class AdjustableSlider extends JPanel implements javax.swing.event.ChangeListener, ActionListener{
//slider for mouse input of values
private JSlider me;
//text box for manual input of values
private JTextField val;
//Make a slider with a textbox on the right and control buttons more right for 1 movement, buttons are stacked.
public AdjustableSlider(int min, int max, int cur, int spacing){
setLayout(new BorderLayout(0, 0));
me = new JSlider();
me.setName("");
me.setMinimum(min);
me.setPaintLabels(true);
me.setPaintTicks(true);
me.setMajorTickSpacing(spacing);
me.setMaximum(max);
me.setValue(cur);
me.addChangeListener(this);
add(me, BorderLayout.CENTER);
JPanel adjusters = new JPanel();
{
adjusters.setLayout(new BorderLayout(0, 0));
val = new JTextField(""+cur, (""+max).length());
val.addActionListener(this);
adjusters.add(val, BorderLayout.CENTER);
JPanel adjustersSub = new JPanel();
{
adjusters.setLayout(new BorderLayout(0, 0));
JButton up = new JButton("+");
up.addActionListener(
new ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt) {
me.setValue(me.getValue()+1);}});
JButton down = new JButton("-");
down.addActionListener(
new ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt) {
me.setValue(me.getValue()-1);}});
adjustersSub.add(up, BorderLayout.NORTH);
adjustersSub.add(down, BorderLayout.SOUTH);
}
adjusters.add(adjustersSub, BorderLayout.EAST);
}
add(adjusters, BorderLayout.EAST);
}
//add a name label to the Slider.
public AdjustableSlider(int min, int max, int cur, int spacing, String name){
this(min, max, cur, spacing);
add(new JLabel(name+":"), BorderLayout.WEST);}
//slider updated, reflect.
public void stateChanged(javax.swing.event.ChangeEvent e){
System.out.println("Slid: " + me.getValue());
val.setText(""+me.getValue());}
//textbox updated, reflect.
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("Type: " + val.getText());
try{int t = Integer.parseInt(val.getText());
if(me.getValue()!=t) me.setValue(t);}
catch(Exception e){e.printStackTrace();}}
//get the current value of this slider
public int get(){
return me.getValue();}
}
我刚开始这个程序,所以我的JFrame非常裸露:
JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setBounds(100, 100, 700, 500);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel sliders = new JPanel();
{
sliders.setLayout(new BorderLayout(0, 0));
}
JPanel controls = new JPanel();
{
controls.setLayout(new BorderLayout(0, 0));
volume = new AdjustableSlider(0, 100, 90, 10, "Volume");
controls.add(volume, BorderLayout.CENTER);
}
frame.add(sliders, BorderLayout.CENTER);
frame.add(controls, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
编辑:我已经做了更正并修复了涉及文本字段更新和更新的一些错误,新代码如下。
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
/*
Made by vzybilly
Look of class:
____________________________________________________
| | | | [+] |
| <Name, if called>: | <Slider> | <Text box> | |
| | | | [-] |
----------------------------------------------------
*/
public class AdjustableSlider extends JPanel{
//slider for mouse input of values
private JSlider me;
//text box for manual input of values
private JTextField val;
//Make a slider with a textbox on the right and control buttons more right for 1 movement, buttons are stacked.
public AdjustableSlider(int min, int max, int cur, int spacing){
setLayout(new BorderLayout(0, 0));
me = new JSlider();
me.setName("");
me.setMinimum(min);
me.setPaintLabels(true);
me.setPaintTicks(true);
me.setMajorTickSpacing(spacing);
me.setMaximum(max);
me.setValue(cur);
me.addChangeListener(
new javax.swing.event.ChangeListener(){
public void stateChanged(javax.swing.event.ChangeEvent e){
try{
if(val.getText().length()<1||Integer.parseInt(val.getText())!=me.getValue())
val.setText(""+me.getValue());}
catch(NumberFormatException nfe){val.setText(""+me.getValue());}}});
add(me, BorderLayout.CENTER);
JPanel adjusters = new JPanel();
{
adjusters.setLayout(new BorderLayout(0, 0));
val = new JTextField(""+cur, (""+max).length());
val.addCaretListener(
new javax.swing.event.CaretListener(){
public void caretUpdate(javax.swing.event.CaretEvent ce){
try{int t = Integer.parseInt(val.getText());
if(me.getValue()!=t) me.setValue(t);}
catch(NumberFormatException nfe){/*System.out.println("null input to box");*/}
catch(Exception e){e.printStackTrace();}}});
adjusters.add(val, BorderLayout.CENTER);
JPanel adjustersSub = new JPanel();
{
adjustersSub.setLayout(new BorderLayout(0, 0));
JButton up = new JButton("+");
up.addActionListener(
new ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt) {
me.setValue(me.getValue()+1);}});
JButton down = new JButton("-");
down.addActionListener(
new ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt) {
me.setValue(me.getValue()-1);}});
adjustersSub.add(up, BorderLayout.NORTH);
adjustersSub.add(down, BorderLayout.SOUTH);
}
adjusters.add(adjustersSub, BorderLayout.EAST);
}
add(adjusters, BorderLayout.EAST);
}
//add a name label to the Slider.
public AdjustableSlider(int min, int max, int cur, int spacing, String name){
this(min, max, cur, spacing);
add(new JLabel(name+":"), BorderLayout.WEST);}
//get the current value of this slider
public int get(){
return me.getValue();}
}
答案 0 :(得分:3)
这是一个拼写错误问题,您的问题是在实例化adjustersSub
后重新定义了adjusters
的布局
49c49
< adjusters.setLayout(new BorderLayout(0, 0));
---
> adjustersSub.setLayout(new BorderLayout(0, 0));