缺少 JTextField 并且布局错误地放置了 JButton
Posted
技术标签:
【中文标题】缺少 JTextField 并且布局错误地放置了 JButton【英文标题】:JTextField missing and Layout wrongly placing JButtons 【发布时间】:2014-02-17 15:58:54 【问题描述】:我正在尝试使用 BorderLayout 将 afew 元素组合到 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());
tryint 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)
tryint 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();
【问题讨论】:
【参考方案1】:这是一个错字问题,您的问题是在实例化 adjustersSub
之后,您重新定义了 adjusters
的布局
49c49
< adjusters.setLayout(new BorderLayout(0, 0));
---
> adjustersSub.setLayout(new BorderLayout(0, 0));
【讨论】:
谢谢,我现在觉得自己很傻...等我重新上网看看能不能上传修改...以上是关于缺少 JTextField 并且布局错误地放置了 JButton的主要内容,如果未能解决你的问题,请参考以下文章