我想添加一個滑塊到我的GUI,但它不會出現,我是新來的java,所以如果你能幫助它將非常感謝!我不知道如何將滑塊添加到主網格,目前它沒有出現。Jslider不會出現在網格上
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class Grid extends JFrame
{
public void Slider()
{
setLayout(new FlowLayout());
JSlider slider;
JLabel label;
slider = new JSlider(JSlider.VERTICAL, 0, 20, 0);
slider.setMajorTickSpacing(5);
slider.setPaintTicks(true);
add(slider);
label = new JLabel ("Number of lifeforms: 0");
add(label);
}
public static void main (String args[])
{
JFrame Grid = new JFrame();
Grid.setSize(800,600);
Grid.setTitle("Artificial life simulator");
Grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String rows = JOptionPane.showInputDialog("How many rows does the grid have?");
int row = Integer.parseInt(rows);
String columns = JOptionPane.showInputDialog("How many columns does the grid have?");
int col = Integer.parseInt(columns);
JOptionPane.showConfirmDialog(null, "Are these the correct demensions: "
+row+" x "+col+ "?",
"Yes or No", JOptionPane.YES_NO_OPTION);
Container pane = Grid.getContentPane();
pane.setLayout(new GridLayout(row,col));
Color square;
for (int x = 1; x <=(row*col); x++)
{
int altr = 0;
altr = (x-1) % col;
altr += (x-1)/col;
if (altr % 2 == 0)
{
square = Color.white;
}
else
{
square = Color.black;
}
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(800/row, 600/col));
panel.setBackground(square);
pane.add(panel);
}
Grid.setVisible(true);
}}
有人嗎?我試着添加一行Grid.Add(Slider);沒有成功 – user3357649