简体   繁体   English

滑块在Java Swing GUI中无法正常工作

[英]Slider not working properly in Java Swing GUI

Though the statements for producing a slider has been put in the code, the slider does not appear in the GUI. 尽管在代码中已经放入了用于生成滑块的语句,但是滑块并未出现在GUI中。 What do I have to modify or add in the code for the slider to appear in the GUI? 我需要修改或添加一些代码以使滑块显示在GUI中? All the other Swing components defined in the program appear except for the slider. 程序中定义的所有其他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);
            }
        }
    }
}

Don't use null layout at all if used then set the bounds for each component that is missing for slider in your code.that's why slider does not appear. 如果使用null布局,请不要使用null布局,然后为代码中的滑块所缺少的每个组件设置边界。这就是为什么滑块不出现的原因。

Use proper layout and redesign your application again. 使用正确的布局,然后重新设计您的应用程序。

It's worth reading A Visual Guide to Layout Managers 值得阅读《布局管理器视觉指南》

EDIT 编辑

With GUI programming, many times, the elements are being rendered but are not seen. 使用GUI编程,很多时候,元素都被渲染但看不到。 For example they are rendered outside of the screen, or outside of the visible portion of the window, or they are rendered but they are hidden by other elements, or they are rendered at a zero size. 例如,它们被渲染到屏幕之外或窗口的可见部分之外,或者被渲染但被其他元素隐藏,或者被零尺寸渲染。

Check if any of these are true for you. 检查其中任何一项对您是否正确。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM