gpt4 book ai didi

java - JSlider 的刻度问题

转载 作者:行者123 更新时间:2023-12-01 17:17:28 33 4
gpt4 key购买 nike

我第一次尝试使用JSlider,并尝试咨询How to Use Sliders学习基础知识。但是当我尝试使刻度线显示为这样时:

 sliderInterior = new JSlider(JSlider.HORIZONTAL,-100,100,0);
sliderInterior.setMinorTickSpacing(5);
sliderInterior.setMajorTickSpacing(10);
sliderInterior.setPaintTicks(true);

蜱虫根本不出现。有什么问题吗?

编辑按要求进行了 SSCCE。问题仍然存在,一个根本没有刻度的 JSlider。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Interface;

import javax.swing.JOptionPane;
import javax.swing.JSlider;

public class JSliderTicks extends javax.swing.JDialog {

public JSliderTicks(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();

slider = new JSlider(JSlider.HORIZONTAL,0,10,5);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(2);
slider.setPaintTicks(true);

JOptionPane.showMessageDialog(null, slider);

setVisible(true);
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

slider = new javax.swing.JSlider();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(85, 85, 85)
.addComponent(slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(115, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(117, 117, 117)
.addComponent(slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(160, 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.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(JSliderTicks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JSliderTicks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JSliderTicks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JSliderTicks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JSliderTicks dialog = new JSliderTicks(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JSlider slider;
// End of variables declaration
}

由于我使用的是 JDialog Form,因此我只更改了构造函数,其余部分会自动实现。

我刚刚注意到,如果我使用 JOptionPane,JSlider 工作正常,但在 JDialog 中它保持不变。

最佳答案

它在这个 SSCCE 中运行得很好。

Slider Ticks

import java.awt.*;
import javax.swing.*;

class SliderTicks {

public static void main(String[] args) {
Runnable r = new Runnable() {

@Override
public void run() {
JSlider sliderInterior = new JSlider(JSlider.HORIZONTAL, -100, 100, 0);
sliderInterior.setMinorTickSpacing(5);
sliderInterior.setMajorTickSpacing(10);
sliderInterior.setPaintTicks(true);

JOptionPane.showMessageDialog(null, sliderInterior);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}

关于java - JSlider 的刻度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20985305/

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