Java Jslider显示数组的值
我正在尝试将显示的值(所选变量)更改为滑块所符合的值。主要问题是我的工作表指定了这一点 displaySelected方法负责只显示所选项目。请查看它,并找出为什么总是显示元素0–它非常简单,而不是复杂 我知道更改显示需要选择变量,我已经尝试过了(g.drawString(names[selected],200150);),但问题是,它只执行数组的值4,并在移动后重置为0,没有,我知道我必须更改selected的值,然后,我尝试了selected=selector.getValue();但是我返回了一个空错误Java Jslider显示数组的值,java,jslider,Java,Jslider,我正在尝试将显示的值(所选变量)更改为滑块所符合的值。主要问题是我的工作表指定了这一点 displaySelected方法负责只显示所选项目。请查看它,并找出为什么总是显示元素0–它非常简单,而不是复杂 我知道更改显示需要选择变量,我已经尝试过了(g.drawString(names[selected],200150);),但问题是,它只执行数组的值4,并在移动后重置为0,没有,我知道我必须更改selected的值,然后,我尝试了selected=selector.getValue();但是我返
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* A simple address database for Practical 5B
* Demonstrates arrays, graphics, selection.
*/
public class Addresses extends JFrame
implements ChangeListener
{
/**
* Frame coordinate constants
*/
private static final int FRAME_X = 200;
private static final int FRAME_Y = 200;
/**
* Frame size constants
*/
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 400;
/**
* The slider for selecting an address record (selectable 0 to size-1).
*/
private JSlider selector;
/**
* Array to hold the database.
*/
private String[] names;
/**
* To indicate which entry is currently selected.
*/
private int selected;
/**
* The drawing panel for display of information.
*/
private JPanel panel;
/**
* Drawing panel size constants
*/
private final int PANEL_WIDTH = 400;
private final int PANEL_HEIGHT = 300;
/**
* The main program launcher for the Addresses class.
*
* @param args The command line arguments (ignored here).
*/
public static void main( String[] args )
{
Addresses frame = new Addresses();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setUpData(); // Initial data set-up
frame.createGUI(); // Initial GUI set-up
frame.setVisible( true );
}
/**
* Sets up the graphical user interface.
*/
private void createGUI()
{
setDefaultCloseOperation( EXIT_ON_CLOSE );
Container window = getContentPane();
window.setLayout( new FlowLayout() );
// Slider for selecting an address entry
selector = new JSlider(JSlider.VERTICAL, 0, 0, 0);
// Note: the useable range of the slider is 0 - 0 at the moment!
window.add(selector);
selector.addChangeListener(this);
// Graphics panel for displaying the address list
panel = new JPanel()
{
// paintComponent is called automatically when a screen refresh is needed
public void paintComponent(Graphics g)
{
// g is a cleared panel area
super.paintComponent(g); // Paint the panel's background
paintScreen(g); // Then the required graphics
}
};
panel.setPreferredSize( new Dimension( PANEL_WIDTH, PANEL_HEIGHT ) );
panel.setBackground( Color.white );
window.add( panel );
}
/**
* Helper method to set up the array data, and the associated variable selected,
* with their initial configuration.
*/
private void setUpData()
{
// All the data is "built-in": set it up here - just one entry at the moment
names = new String[6]; // Create the array with space for one entry
names[0] = "James"; // The entry
names[1] = "Connor";
names[2] = "Alfred";
names[3] = "Billy";
names[4] = "Jack";
names[5] = "Chris";
selected = names.length-1; // Indicate that entry 0 is selected
}
/**
* This methods redraws the screen.
*/
private void paintScreen(Graphics g)
{
displayList(g);
displaySelected(g);
}
/**
* Display all the elements of array names in a column on the screen.
*/
private void displayList(Graphics g)
{
int y = 100; // Top y coordinate of the column
g.setColor(Color.black);
/*
g.drawString(names[0], 20, y);
g.drawString(names[1], 20, y+25);
g.drawString(names[2], 20, y+50);
g.drawString(names[3], 20, y+75);
g.drawString(names[4], 20, y+100);
g.drawString(names[5], 20, y+125);
*/
for (int i = 0; i< names.length; i++)
g.drawString(names[i], 20, y+25*i);
}
/**
* Display the single element of array names that is currently selected by the slider.
*/
private void displaySelected(Graphics g)
{
g.setColor(Color.black);
g.drawString("Current selection is:", 200, 135);
g.drawString(names[selected], 200, 150);
}
/**
* Reacts to adjustment of the slider.
* Notes the new selector setting, then forces screen refresh.
*/
public void stateChanged( ChangeEvent e )
{
// selector has been adjusted: record the new setting
selected = selector.getValue();
repaint(); // Refresh the screen
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.*;
/**
*实用5B的简易地址数据库
*演示阵列、图形和选择。
*/
公共类地址扩展JFrame
实现ChangeListener
{
/**
*框架坐标常数
*/
私有静态最终整数帧_X=200;
私有静态最终整数帧_Y=200;
/**
*帧大小常数
*/
专用静态最终整数帧_宽度=500;
专用静态最终整型框架高度=400;
/**
*用于选择地址记录的滑块(可选择0到大小1)。
*/
私有JSlider选择器;
/**
*数组来保存数据库。
*/
私有字符串[]名称;
/**
*指示当前选择的条目。
*/
选择私人int;
/**
*用于显示信息的图形面板。
*/
私人JPanel小组;
/**
*图形面板大小常量
*/
专用最终内部面板_宽度=400;
专用最终内部面板高度=300;
/**
*Addresses类的主程序启动器。
*
*@param指定命令行参数(此处忽略)。
*/
公共静态void main(字符串[]args)
{
地址帧=新地址();
框。设置尺寸(框宽、框高);
frame.setUpData();//初始数据设置
frame.createGUI();//初始GUI设置
frame.setVisible(true);
}
/**
*设置图形用户界面。
*/
私有void createGUI()
{
setDefaultCloseOperation(关闭时退出);
容器窗口=getContentPane();
setLayout(新的FlowLayout());
//用于选择地址项的滑块
选择器=新的JSlider(JSlider.VERTICAL,0,0,0);
//注意:此时滑块的可用范围为0-0!
添加(选择器);
selector.addChangeListener(此);
//用于显示地址列表的图形面板
panel=newjpanel()
{
//当需要刷新屏幕时,会自动调用paintComponent
公共组件(图形g)
{
//g是一个已清除的面板区域
super.paintComponent(g);//绘制面板的背景
paintScreen(g);//然后是所需的图形
}
};
面板。设置首选尺寸(新尺寸(面板宽度、面板高度));
面板。立根背景(颜色为白色);
窗口。添加(面板);
}
/**
*用于设置数组数据的Helper方法,以及所选的关联变量,
*与他们的初始配置。
*/
私有void setUpData()
{
//所有的数据都是“内置的”:在这里设置-目前只有一个条目
名称=新字符串[6];//创建一个数组,其中一个条目有空格
名称[0]=“James”;//条目
名称[1]=“康纳”;
姓名[2]=“阿尔弗雷德”;
姓名[3]=“比利”;
姓名[4]=“杰克”;
姓名[5]=“克里斯”;
selected=names.length-1;//表示选择了条目0
}
/**
*此方法会重新绘制屏幕。
*/
专用虚空漆幕(图形g)
{
显示列表(g);
显示所选(g);
}
/**
*在屏幕上的列中显示数组名称的所有元素。
*/
私有void显示列表(图g)
{
int y=100;//列的顶部y坐标
g、 设置颜色(颜色为黑色);
/*
g、 抽绳(名称[0],20,y);
g、 抽绳(名称[1],20,y+25);
g、 抽绳(名称[2],20,y+50);
g、 抽绳(名称[3],20,y+75);
g、 抽绳(名称[4],20,y+100);
g、 抽绳(名称[5],20,y+125);
*/
for(int i=0;i
您从未设置滑块的最大值。这就是为什么你不能移动它。对代码进行此更改,然后就可以调试此程序的其余部分
selector = new JSlider(JSlider.VERTICAL, 0, names.length-1 /* HERE */ , 0);