【问题标题】:Jslider, not appearing on My frameJslider,没有出现在我的框架上
【发布时间】:2012-10-20 01:05:22
【问题描述】:

一旦我运行我的 java 项目,我试图让我的滑块出现在我的 gui 上,这就是我所拥有的,任何帮助都会很好:)

所以菜单栏出现了,菜单形状出现了,3 个形状也出现了,我只是不能>让滑块出现在框的底部


课堂作业


package assignment;

public class Assignment {

public static void main(String[] args) {
    
MyFrame pane = new MyFrame();  
   
pane.setVisible(true);
pane.setSize(400,400);

类 MyFrame


package assignment;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MyFrame extends javax.swing.JFrame {

/**
 * Creates new form MyFrame
 */
public MyFrame() {
initComponents();
    
// Create the menu
JMenuBar topMenu = new JMenuBar();
this.setJMenuBar(topMenu);
    
//create the menu button "shapes"
JMenu shapes = new JMenu("Shapes");
topMenu.add(shapes);
//Create the 3 shapes for the menu
JMenuItem square = new JMenuItem("Square");
JMenuItem circle = new JMenuItem("Circle");        
JMenuItem triangle = new JMenuItem("Triangle");
    
//add shapes to menu
shapes.add(circle);
shapes.add(triangle);
shapes.add(square);
    
//add the menu
setJMenuBar(topMenu);
    
    
MyControlPanel pane = new MyControlPanel();
this.add(pane);
    
    
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info :          javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
        new MyFrame().setVisible(true);
        }
    });
    
    
}
// Variables declaration - do not modify
// End of variables declaration

}


类 MyControlPanel


package assignment;

import javax.swing.JSlider;
import javax.swing.JTextField;


public class MyControlPanel extends javax.swing.JPanel {

/**
 * Creates new form MyControlPanel
 */
public MyControlPanel() {
initComponents();

JSlider slider = new JSlider();
slider.setMajorTickSpacing(10); 
slider.setPaintLabels(true); 
slider.setPaintTicks(true); 
 
JTextField boundary_length = new JTextField("Boundary Length");
JTextField area = new JTextField("Area");
    
 
this.add(slider);
this.add(area);
this.add(boundary_length);
    
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration

}

【问题讨论】:

  • 1.在您了解各种布局管理器在做什么之前,请避免使用表单设计器。 2.使用不同的布局管理器,尝试FormLayout作为测试。查看A Visual Guide to Layout ManagersUsing Layout Managers 了解更多信息
  • 我明白这一点,但我确定我在某些地方遗漏了一些代码?
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • @StevenMcilhone 你试过用FlowLayout 来确认组件出现了吗?
  • @Steven:您只是将 JSlider 添加到 GUI 中,根本不考虑布局管理器。我赞同其他人所说的话。转到layout manager tutorial 并通读它,然后删除您生成的代码。您需要了解布局的作用以及如何使用它们,然后才能有人帮助您。

标签: java swing jframe layout-manager jslider


【解决方案1】:

你缺少的是对GroupLayout的理解

如果我这样做

public MyControlPanel() {
    initComponents();

    JSlider slider = new JSlider();
    slider.setMajorTickSpacing(10);
    slider.setPaintLabels(true);
    slider.setPaintTicks(true);

    JTextField boundary_length = new JTextField("Boundary Length");
    JTextField area = new JTextField("Area");

    setLayout(new FlowLayout());

    this.add(slider);
    this.add(area);
    this.add(boundary_length);

}

我明白了……

我所做的只是将布局管理器更改为FlowLayout。无意冒犯,但 GroupLayout 不应该是手动创建的,因为它确实应该与表单编辑器一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多