gpt4 book ai didi

java - JTextField 缺失且布局错误地放置了 JButton

转载 作者:行者123 更新时间:2023-12-01 23:15:00 25 4
gpt4 key购买 nike

我正在尝试使用 BorderLayout 将一些元素组合到 JPanel 中。我在中心有一个 JSlider,如果调用该构造函数,则在西边有一个名称。然后在东边,我有另一个用于控制 slider 的面板,该面板给出了所有问题。

在东面板上,我想要一个 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();}
}

最佳答案

这是一个拼写错误问题,您的问题是在实例化 adjustersSub 后,您重新定义了 adjusters 的布局

49c49
< adjusters.setLayout(new BorderLayout(0, 0));
---
> adjustersSub.setLayout(new BorderLayout(0, 0));

关于java - JTextField 缺失且布局错误地放置了 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21370260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com