gpt4 book ai didi

java - 为什么我的 JSlider 没有显示在我的 java GUI 中?

转载 作者:行者123 更新时间:2023-11-30 03:13:46 24 4
gpt4 key购买 nike

我真的很沮丧,因为 GUI 显示但没有 JSlider!好的,GUI 应该显示一个圆圈,JTextfield(当 slider 移动时更新)。文本字段应该显示半径、直径等,并在移动 slider 时自动更新。一旦 slider 移动,圆圈的尺寸也会增大或减小。这是我的代码。没有错误,运行良好,并且可以编译。可能是我的坐标出了问题。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import javafx.stage.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Circle1 extends JFrame
{

private final CircleCanvas theCanvas;

private final JTextArea display;

public Circle1()

{

super( "Circle1" );

theCanvas = new CircleCanvas();

display = new JTextArea( 4, 30 );



display.setText( "The Radius: " + theCanvas.getRadius() + "\nThe Diameter: " + theCanvas.getDiameter() + "\nThe Area: " + theCanvas.getArea() +

"\nThe Circumference: " + theCanvas.getCircumference() );



getContentPane().add( theCanvas, BorderLayout.CENTER );

getContentPane().add( display, BorderLayout.SOUTH );

setSize( 200, 200 );
setVisible(true);

}



public static void main( String args[] )

{

Circle1 app = new Circle1();


app.addWindowListener(

new WindowAdapter()
{

public void WindowClosing( WindowEvent e )

{

}
}
);
}

void setDiameter(int value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

class CircleCanvas extends JPanel
{

private final int radius;

public CircleCanvas()

{

radius = ( int )( 1 + Math.random() * 100 );

setSize (100, 100);

}

@Override
public void paintComponent( Graphics g )

{ g.drawOval( 0, 0, radius, radius ); }



public int getDiameter()
{ return ( 2 * radius ); }



public int getCircumference()

{ return ( int )( 2 * Math.PI * radius ); }



public int getArea()

{ return ( int )( radius * radius * Math.PI ); }



public int getRadius()
{ return radius; }

}

class SliderFrame extends JFrame
{
private JSlider diameterJSlider;
private Circle1 myPanel;

public SliderFrame()
{

super("HW2");

myPanel = new Circle1();


diameterJSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 300, 10);
diameterJSlider.setMajorTickSpacing(10);
diameterJSlider.setPaintTicks(true);
diameterJSlider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
myPanel.setDiameter(diameterJSlider.getValue());
}
}
);
add(diameterJSlider, BorderLayout.NORTH);
add(myPanel, BorderLayout.CENTER);
}
}

最佳答案

Circle1 是一个窗口,它不能添加到另一个窗口,所以你的 SliderFrame 已经做错了,这就是为什么通常不鼓励直接扩展从像 JFrame 这样的顶级容器开始,你应该从像 JPanel

这样的东西开始

所以,你可以从......开始

public class Circle1 extends JPanel {

private final CircleCanvas theCanvas;

private final JTextArea display;

public Circle1() {

setLayout(new BorderLayout());

theCanvas = new CircleCanvas();

display = new JTextArea(4, 30);

display.setText("The Radius: " + theCanvas.getRadius() + "\nThe Diameter: " + theCanvas.getDiameter() + "\nThe Area: " + theCanvas.getArea()
+ "\nThe Circumference: " + theCanvas.getCircumference());

add(theCanvas, BorderLayout.CENTER);
add(display, BorderLayout.SOUTH);

}

void setDiameter(int value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

class CircleCanvas extends JPanel {

private final int radius;

public CircleCanvas() {

radius = (int) (1 + Math.random() * 100);

}

@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(0, 0, radius, radius);
}

public int getDiameter() {
return (2 * radius);
}

public int getCircumference() {
return (int) (2 * Math.PI * radius);
}

public int getArea() {
return (int) (radius * radius * Math.PI);
}

public int getRadius() {
return radius;
}

}
}

然后你可以使用类似的东西......

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Circle1());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});

显示。

然后您可以将 SliderFrame 更改为更像...的内容

public class SliderPane extends JPanel {

private JSlider diameterJSlider;
private Circle1 myPanel;

public SliderPane() {

setLayout(new BorderLayout());

myPanel = new Circle1();

diameterJSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 300, 10);
diameterJSlider.setMajorTickSpacing(10);
diameterJSlider.setPaintTicks(true);
diameterJSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
myPanel.setDiameter(diameterJSlider.getValue());
}
}
);
add(diameterJSlider, BorderLayout.NORTH);
add(myPanel, BorderLayout.CENTER);
}
}

并使用类似...的东西

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SliderPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});

展示它。

Slider

可运行示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Change these two lines to switch between the panels
//frame.add(new Circle1());
frame.add(new SliderPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class Circle1 extends JPanel {

private final CircleCanvas theCanvas;

private final JTextArea display;

public Circle1() {

setLayout(new BorderLayout());

theCanvas = new CircleCanvas();

display = new JTextArea(4, 30);

display.setText("The Radius: " + theCanvas.getRadius() + "\nThe Diameter: " + theCanvas.getDiameter() + "\nThe Area: " + theCanvas.getArea()
+ "\nThe Circumference: " + theCanvas.getCircumference());

add(theCanvas, BorderLayout.CENTER);
add(display, BorderLayout.SOUTH);

}

void setDiameter(int value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

class CircleCanvas extends JPanel {

private final int radius;

public CircleCanvas() {

radius = (int) (1 + Math.random() * 100);

}

@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(0, 0, radius, radius);
}

public int getDiameter() {
return (2 * radius);
}

public int getCircumference() {
return (int) (2 * Math.PI * radius);
}

public int getArea() {
return (int) (radius * radius * Math.PI);
}

public int getRadius() {
return radius;
}

}
}

public class SliderPane extends JPanel {

private JSlider diameterJSlider;
private Circle1 myPanel;

public SliderPane() {

setLayout(new BorderLayout());

myPanel = new Circle1();

diameterJSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 300, 10);
diameterJSlider.setMajorTickSpacing(10);
diameterJSlider.setPaintTicks(true);
diameterJSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
myPanel.setDiameter(diameterJSlider.getValue());
}
}
);
add(diameterJSlider, BorderLayout.NORTH);
add(myPanel, BorderLayout.CENTER);
}
}

}

关于java - 为什么我的 JSlider 没有显示在我的 java GUI 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092007/

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