虽然代码中包含了生成滑块的语句,但该滑块并未出现在GUI中。要在GUI中显示滑块,我必须在代码中修改或添加什么?程序中定义的所有其他Swing组件都出现了,除了滑块。
package pkTopic5T15;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Topic5T15 {
public int top1 = 60;
public int top2 = 300;
public JPanel pnlLeft;
public JPanel pnlRight;
public int Initialht1 = 100;
public int Initialht2 = 100;
public JSlider sldChangePanels;
public static void main(String[] args) {
Topic5T15 My515 = new Topic5T15();
My515.go();
}
public void go() {
GUI515 My515 = new GUI515();
}
class GUI515 extends JFrame {
private JLabel lblHeading;
private JButton btnPanLeft;
private JButton btnPanRight;
public int ht1;
public int ht2;
protected GUI515() {
this.setSize(800, 900);
this.setLocation(100, 100);
this.setTitle("515");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
Font f1 = new Font("Monospaced", Font.BOLD, 16);
Font f2 = new Font("Dialog", Font.BOLD, 24);
lblHeading = new JLabel("Testing Panels");
lblHeading.setBounds(10, 10, 300, 50);
lblHeading.setFont(f2);
pnlLeft = new JPanel();
pnlLeft.setLayout(null);
pnlLeft.setBounds(10, top1, 300, Initialht1);
pnlLeft.setBackground(Color.RED);
btnPanLeft = new JButton("One");
btnPanLeft.setFont(f1);
btnPanLeft.setBounds(0, 0, 100, 50);
ClickOne c = new ClickOne();
btnPanLeft.addActionListener(c);
pnlLeft.add(btnPanLeft);
this.add(pnlLeft);
pnlRight = new JPanel();
pnlRight.setLayout(null);
pnlRight.setBounds(350, top2, 300, Initialht2);
pnlRight.setBackground(Color.GREEN);
btnPanRight = new JButton("Two");
btnPanRight.setBounds(0, 0, 100, 50);
btnPanRight.setFont(f1);
btnPanRight.addActionListener(c);
pnlRight.add(btnPanRight);
this.add(pnlRight);
sldChangePanels = new JSlider(-100, 100, 1);
sldChangePanels.setBounds(50, 800, 400, 50);
sldChangePanels.setMajorTickSpacing(20);
sldChangePanels.setPaintLabels(true);
SliderListener slis = new SliderListener();
sldChangePanels.addChangeListener(slis);
this.add(sldChangePanels);
this.add(lblHeading);
this.setVisible(true);
}
private class ClickOne implements ActionListener
// This is an inner class; contained within GUI
{
protected ClickOne() {
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnPanLeft) {
btnPanLeft.setText("OUCH1");
top1 = top1 + 20;
pnlLeft.setBounds(10, top1, 300, 100);
}
if (e.getSource() == btnPanRight) {
btnPanRight.setText("OUCH2");
top2 = top2 - 20;
pnlRight.setBounds(350, top2, 300, 100);
}
}
}
private class SliderListener implements ChangeListener
// This is an inner class; contained within GUI
{
protected SliderListener() {
}
public void stateChanged(ChangeEvent e) {
ht1 = Initialht1 + sldChangePanels.getValue();
ht2 = Initialht2 - sldChangePanels.getValue();
pnlLeft.setBounds(10, top1, 300, ht1);
pnlRight.setBounds(350, top2, 300, ht2);
}
}
}
}
不要使用null
布局,如果使用,然后设置每个组件的边界是缺失的滑块在你的代码。这就是为什么滑块没有出现
使用适当的布局,重新设计应用程序
布局管理器视觉指南
编辑
-
使用
SwingUtilities.invokeLater()
确保EDT正确初始化。阅读更多
-
为什么要使用swingutility。invokeLater in main method?
-
SwingUtilities.invokeLater
-
对于GUI编程,很多时候,元素被呈现,但不被看到。例如,它们被渲染在屏幕之外,或者在窗口的可见部分之外,或者它们被渲染但被其他元素隐藏,或者它们以零大小渲染。
检查这些是否为真。
HTH .