標題:
JSlider 類別
[打印本頁]
作者:
周政輝
時間:
2018-5-5 11:30
標題:
JSlider 類別
本帖最後由 周政輝 於 2018-5-5 12:03 編輯
利用 JSlider 元件,設計一個簡單的 rgb 調色盤如圖所示。
[attach]4007[/attach]
[attach]4008[/attach]
本帖隱藏的內容需要回復才可以瀏覽
作者:
蔡庭豪
時間:
2018-5-5 12:03
本帖最後由 蔡庭豪 於 2018-5-12 10:25 編輯
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.Color;
public class RGB extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RGB frame = new RGB();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RGB() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblRed = new JLabel("red");
lblRed.setForeground(Color.RED);
lblRed.setBounds(10, 192, 21, 15);
contentPane.add(lblRed);
JLabel lblGreen = new JLabel("green");
lblGreen.setForeground(Color.GREEN);
lblGreen.setBounds(10, 215, 36, 15);
contentPane.add(lblGreen);
JLabel lblBlue = new JLabel("blue");
lblBlue.setForeground(Color.BLUE);
lblBlue.setBounds(10, 237, 21, 15);
contentPane.add(lblBlue);
JLabel gl = new JLabel();
gl.setText("0");
gl.setBounds(403, 207, 21, 23);
contentPane.add(gl);
JLabel bl = new JLabel();
bl.setText("0");
bl.setBounds(403, 229, 21, 23);
contentPane.add(bl);
JLabel rl = new JLabel();
rl.setText("0");
rl.setBounds(403, 184, 21, 23);
contentPane.add(rl);
JSlider sliderred = new JSlider();
sliderred.setValue(0);
sliderred.setMaximum(255);
sliderred.setBounds(56, 184, 343, 23);
contentPane.add(sliderred);
JSlider slidergreen = new JSlider();
slidergreen.setValue(0);
slidergreen.setMaximum(255);
slidergreen.setBounds(56, 207, 343, 23);
contentPane.add(slidergreen);
JSlider sliderblue = new JSlider();
sliderblue.setValue(0);
sliderblue.setMaximum(255);
sliderblue.setBounds(56, 229, 343, 23);
contentPane.add(sliderblue);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 414, 172);
contentPane.add(panel);
sliderblue.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
bl.setText(String.valueOf(sliderblue.getValue()));
Color color = new Color(sliderred.getValue(), slidergreen.getValue(),sliderblue.getValue());
panel.setBackground(color);
}
});
slidergreen.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
gl.setText(String.valueOf(slidergreen.getValue()));
Color color = new Color(sliderred.getValue(), slidergreen.getValue(),sliderblue.getValue());
panel.setBackground(color);
}
});
sliderred.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
Color color = new Color(sliderred.getValue(), slidergreen.getValue(),sliderblue.getValue());
panel.setBackground(color);
}
});
}
}
複製代碼
作者:
蔡季樺
時間:
2018-5-5 12:05
本帖最後由 蔡季樺 於 2018-5-6 13:33 編輯
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.Color;
public class Main extends JFrame {
private JPanel blue;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
blue = new JPanel();
blue.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(blue);
blue.setLayout(null);
JSlider b = new JSlider();
b.setValue(0);
b.setMaximum(255);
b.setBounds(10, 197, 200, 23);
blue.add(b);
JLabel green = new JLabel("0");
green.setForeground(Color.BLUE);
green.setBounds(307, 205, 46, 15);
blue.add(green);
JSlider g = new JSlider();
g.setValue(0);
g.setMaximum(255);
g.setBounds(10, 142, 200, 23);
blue.add(g);
JLabel label2 = new JLabel("0");
label2.setForeground(Color.GREEN);
label2.setBounds(307, 150, 46, 15);
blue.add(label2);
JSlider r = new JSlider();
r.setValue(0);
r.setMaximum(255);
r.setBounds(10, 95, 200, 23);
blue.add(r);
JLabel red = new JLabel("0");
red.setForeground(Color.RED);
red.setBounds(307, 95, 46, 15);
blue.add(red);
JPanel panel = new JPanel();
panel.setBounds(183, 10, 67, 63);
blue.add(panel);
b.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
green.setText(String.valueOf(b.getValue()));
Color color = new Color(r.getValue(),g.getValue(),b.getValue());
panel.setBackground(color);
}
});
g.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
label2.setText(String .valueOf(g.getValue()));
Color color = new Color(r.getValue(),g.getValue(),b.getValue());
panel.setBackground(color);
}
});
r.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
red.setText(String .valueOf(r.getValue()));
Color color = new Color(r.getValue(),g.getValue(),b.getValue());
panel.setBackground(color);
}
});
}
}
複製代碼
提醒大家
Jlabel & Jframe
無法設定背景
(需用Jpanel)
作者:
林侑成
時間:
2018-5-12 10:50
package asdf;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JSlider;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JPanel;
public class Main {
private JFrame frmRgb;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frmRgb.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmRgb = new JFrame();
frmRgb.setTitle("RGB\u8ABF\u8272\u5DE5\u5177");
frmRgb.setBounds(100, 100, 413, 284);
frmRgb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmRgb.getContentPane().setLayout(null);
JSlider G = new JSlider();
frmRgb.getContentPane().add(G);
G.setValue(0);
G.setMaximum(255);
G.setBounds(37, 110, 261, 29);
G.setMaximum(255);
JSlider B = new JSlider();
frmRgb.getContentPane().add(B);
B.setValue(0);
B.setMaximum(255);
B.setBounds(37, 204, 261, 32);
B.setMaximum(255);
JSlider R = new JSlider();
frmRgb.getContentPane().add(R);
R.setValue(0);
R.setMaximum(255);
R.setBounds(37, 159, 261, 29);
R.setMaximum(255);
JLabel green = new JLabel("0");
frmRgb.getContentPane().add(green);
green.setForeground(Color.GREEN);
green.setBounds(329, 100, 47, 51);
JLabel red = new JLabel("0");
frmRgb.getContentPane().add(red);
red.setForeground(Color.RED);
red.setBounds(329, 159, 47, 29);
JLabel blue = new JLabel("0");
frmRgb.getContentPane().add(blue);
blue.setForeground(Color.BLUE);
blue.setBounds(329, 204, 30, 21);
JPanel panel = new JPanel();
panel.setBounds(113, 10, 126, 90);
frmRgb.getContentPane().add(panel);
B.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
blue.setText(String.valueOf(B.getValue()));
Color color = new Color(R.getValue(),G.getValue(),B.getValue());
panel.setBackground(color);
}
});
R.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
red.setText(String.valueOf(R.getValue()));
Color color = new Color(R.getValue(),G.getValue(),B.getValue());
panel.setBackground(color);
}
});
G.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
green.setText(String.valueOf(G.getValue()));
Color color = new Color(R.getValue(),G.getValue(),B.getValue());
panel.setBackground(color);
}
});
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-5-12 10:54
package tw.kuas.edu.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSlider;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class RGB extends JFrame {
private JPanel contentPane;
int r;
int g;
int b;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RGB frame = new RGB();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RGB() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel bground = new JPanel();
bground.setBounds(10, 10, 414, 114);
contentPane.add(bground);
JLabel label_r = new JLabel("0");
label_r.setFont(new Font("Consolas", Font.BOLD, 18));
label_r.setForeground(Color.RED);
label_r.setBounds(364, 150, 60, 23);
contentPane.add(label_r);
JLabel label_g = new JLabel("0");
label_g.setForeground(Color.GREEN);
label_g.setFont(new Font("Consolas", Font.BOLD, 18));
label_g.setBounds(364, 183, 60, 23);
contentPane.add(label_g);
JLabel label_b = new JLabel("0");
label_b.setForeground(Color.BLUE);
label_b.setFont(new Font("Consolas", Font.BOLD, 18));
label_b.setBounds(364, 216, 60, 23);
contentPane.add(label_b);
JSlider slider_r = new JSlider();
JSlider slider_g = new JSlider();
JSlider slider_b = new JSlider();
slider_r.setMaximum(255);
slider_r.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
label_r.setText(String.valueOf(slider_r.getValue()));
Color color = new Color(slider_r.getValue(),slider_g.getValue(),slider_b.getValue());
bground.setBackground(color);
}
});
slider_r.setValue(0);
slider_r.setBounds(10, 150, 344, 23);
contentPane.add(slider_r);
slider_g.setValue(0);
slider_g.setMaximum(255);
slider_g.setBounds(10, 183, 344, 23);
contentPane.add(slider_g);
slider_g.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
label_g.setText(String.valueOf(slider_g.getValue()));
Color color = new Color(slider_r.getValue(),slider_g.getValue(),slider_b.getValue());
bground.setBackground(color);
}
});
slider_b.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
label_b.setText(String.valueOf(slider_b.getValue()));
Color color = new Color(slider_r.getValue(),slider_g.getValue(),slider_b.getValue());
bground.setBackground(color);
}
});
slider_b.setValue(0);
slider_b.setMaximum(255);
slider_b.setBounds(10, 216, 344, 23);
contentPane.add(slider_b);
}
}
複製代碼
作者:
黃茂勛
時間:
2018-5-23 17:01
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Color;
import javax.swing.JSlider;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
public class Main extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JSlider slider1;
private JSlider slider2;
private JSlider slider3;
private JLabel lb_r;
private JLabel lb_g;
private JLabel lb_b;
private JPanel panel;
private int r,g,b;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
frame.setTitle("調色工具");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 435, 403);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setText("0,0,0");
textField.setBounds(164, 10, 96, 21);
contentPane.add(textField);
textField.setColumns(10);
panel = new JPanel();
panel.setBackground(new Color(0, 0, 0));
panel.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.setBounds(149, 41, 126, 128);
contentPane.add(panel);
slider1 = new JSlider();
slider1.setValue(0);
slider1.setMaximum(255);
slider1.setBounds(10, 193, 276, 23);
slider1.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e)
{
lb_r.setText(slider1.getValue()+"");
r=slider1.getValue();
panel.setBackground(new Color(r, g, b));
textField.setText(r+","+g+","+b);
}
});
contentPane.add(slider1);
slider2 = new JSlider();
slider2.setValue(0);
slider2.setMaximum(255);
slider2.setBounds(10, 254, 276, 23);
slider2.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e)
{
lb_g.setText(slider2.getValue()+"");
g=slider2.getValue();
panel.setBackground(new Color(r, g, b));
textField.setText(r+","+g+","+b);
}
});
contentPane.add(slider2);
slider3 = new JSlider();
slider3.setValue(0);
slider3.setMaximum(255);
slider3.setBounds(10, 312, 276, 23);
slider3.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e)
{
lb_b.setText(slider3.getValue()+"");
b=slider3.getValue();
panel.setBackground(new Color(r, g, b));
textField.setText(r+","+g+","+b);
}
});
contentPane.add(slider3);
lb_r = new JLabel("0");
lb_r.setFont(new Font("新細明體", Font.BOLD, 16));
lb_r.setForeground(Color.RED);
lb_r.setBounds(318, 193, 57, 23);
contentPane.add(lb_r);
lb_g = new JLabel("0");
lb_g.setForeground(new Color(0, 100, 0));
lb_g.setFont(new Font("新細明體", Font.BOLD, 16));
lb_g.setBounds(318, 254, 57, 23);
contentPane.add(lb_g);
lb_b = new JLabel("0");
lb_b.setForeground(new Color(0, 0, 139));
lb_b.setFont(new Font("新細明體", Font.BOLD, 16));
lb_b.setBounds(318, 312, 57, 23);
contentPane.add(lb_b);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2