如何用java图形用户界面实现下面的功能:1.运行时出现如下面板 2.单击改变字体大小时可以使得字体大小加2
3.点击设置字体时,出现选择字体和字形的对话框4单击打开调色板按钮出现调色板对话框。。急急急,求大神帮忙写个程序,可加悬赏...
3.点击设置字体时,出现选择字体和字形的对话框 4单击打开调色板按钮出现调色板对话框。。急急急,求大神帮忙写个程序,可加悬赏
展开
2个回答
展开全部
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HelpYou extends JFrame{
public JButton up = new JButton("改变字体大小");
public JButton left = new JButton("设置字体");
public JButton down = new JButton("打开调色面板");
public JTextArea area = new JTextArea("这是有一个文本区,用来输入文本");
public int fontSize = 12;//设置默认字体大小为12
public JPanel panel = new JPanel();
public Object SetFont;
public HelpYou(){
innit();
}
public void innit(){//初始化面板
BorderLayout bl = new BorderLayout();
panel.setLayout(bl);
panel.add(up,bl.NORTH);
panel.add(left,bl.WEST);
panel.add(down,bl.SOUTH);
panel.add(area,bl.CENTER);
area.setLineWrap(true);//设置文本域自动换行
up.addActionListener(new MyClick());
left.addActionListener(new MyClick());
down.addActionListener(new MyClick());
this.setContentPane(panel);
this.setTitle("My Java");
this.setSize(400,300);
this.setVisible(true);
this.setResizable(false);//设置窗体大小不可改
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MyClick implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==up){
fontSize+=2;
area.setFont(new Font("宋体",Font.PLAIN,fontSize));
}
if(e.getSource()==left){
SetFont sf = new SetFont();
}
if(e.getSource()==down){
SetColor sc = new SetColor();
}
}
}
class SetColor extends JFrame{//设置字体的面板
public JButton ok = new JButton("确定");
public JButton cancel = new JButton("取消");
public JPanel panel = new JPanel();
public SetColor(){
innit();
}
public void innit(){
JPanel colorPanel = new JPanel();
colorPanel.setLayout(new GridLayout(1, 8));//一共设置了7种颜色
JButton[] cButton = new JButton[8];
for(int i=0;i<8;i++){
cButton[i] = new JButton();//对颜色按钮初始化
colorPanel.add(cButton[i]);
}
cButton[0].setBackground(Color.RED);
cButton[1].setBackground(Color.ORANGE);
cButton[2].setBackground(Color.YELLOW);
cButton[3].setBackground(Color.GREEN);
cButton[4].setBackground(Color.BLUE);
cButton[5].setBackground(Color.CYAN);
cButton[6].setBackground(Color.PINK);
cButton[7].setBackground(Color.BLACK);
colorPanel.add(cButton[0]);
colorPanel.setLayout(new FlowLayout());//设置默认布局
panel.add(colorPanel);
panel.add(ok);
panel.add(cancel);
this.setSize(350,120);
this.setLocation(0,50);
this.setContentPane(panel);
this.setVisible(true);
this.setResizable(false);//设置窗体大小不可改
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class SetFont extends JFrame{//设置字体的面板
public JButton ok = new JButton("确定");
public JButton cancel = new JButton("取消");
public JComboBox size = new JComboBox();
public JComboBox type = new JComboBox();
public JPanel fontPanel = new JPanel();
public SetFont(){
innit();
}
public void innit(){
fontPanel.setLayout(new FlowLayout());//设置默认布局
fontPanel.add(size);
fontPanel.add(type);
fontPanel.add(ok);
fontPanel.add(cancel);
size.addItem("选择字体");
size.addItem("12");
size.addItem("20");
size.addItem("30");
size.addItem("50");
type.addItem("选择字形");
type.addItem("宋体");
type.addItem("黑体");
type.addItem("微软雅黑");
this.setSize(400,100);
this.setLocation(0,100);
this.setContentPane(fontPanel);
this.setVisible(true);
this.setResizable(false);//设置窗体大小不可改
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
HelpYou hy = new HelpYou();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HelpYou extends JFrame{
public JButton up = new JButton("改变字体大小");
public JButton left = new JButton("设置字体");
public JButton down = new JButton("打开调色面板");
public JTextArea area = new JTextArea("这是有一个文本区,用来输入文本");
public int fontSize = 12;//设置默认字体大小为12
public JPanel panel = new JPanel();
public Object SetFont;
public HelpYou(){
innit();
}
public void innit(){//初始化面板
BorderLayout bl = new BorderLayout();
panel.setLayout(bl);
panel.add(up,bl.NORTH);
panel.add(left,bl.WEST);
panel.add(down,bl.SOUTH);
panel.add(area,bl.CENTER);
area.setLineWrap(true);//设置文本域自动换行
up.addActionListener(new MyClick());
left.addActionListener(new MyClick());
down.addActionListener(new MyClick());
this.setContentPane(panel);
this.setTitle("My Java");
this.setSize(400,300);
this.setVisible(true);
this.setResizable(false);//设置窗体大小不可改
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MyClick implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==up){
fontSize+=2;
area.setFont(new Font("宋体",Font.PLAIN,fontSize));
}
if(e.getSource()==left){
SetFont sf = new SetFont();
}
if(e.getSource()==down){
SetColor sc = new SetColor();
}
}
}
class SetColor extends JFrame{//设置字体的面板
public JButton ok = new JButton("确定");
public JButton cancel = new JButton("取消");
public JPanel panel = new JPanel();
public SetColor(){
innit();
}
public void innit(){
JPanel colorPanel = new JPanel();
colorPanel.setLayout(new GridLayout(1, 8));//一共设置了7种颜色
JButton[] cButton = new JButton[8];
for(int i=0;i<8;i++){
cButton[i] = new JButton();//对颜色按钮初始化
colorPanel.add(cButton[i]);
}
cButton[0].setBackground(Color.RED);
cButton[1].setBackground(Color.ORANGE);
cButton[2].setBackground(Color.YELLOW);
cButton[3].setBackground(Color.GREEN);
cButton[4].setBackground(Color.BLUE);
cButton[5].setBackground(Color.CYAN);
cButton[6].setBackground(Color.PINK);
cButton[7].setBackground(Color.BLACK);
colorPanel.add(cButton[0]);
colorPanel.setLayout(new FlowLayout());//设置默认布局
panel.add(colorPanel);
panel.add(ok);
panel.add(cancel);
this.setSize(350,120);
this.setLocation(0,50);
this.setContentPane(panel);
this.setVisible(true);
this.setResizable(false);//设置窗体大小不可改
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class SetFont extends JFrame{//设置字体的面板
public JButton ok = new JButton("确定");
public JButton cancel = new JButton("取消");
public JComboBox size = new JComboBox();
public JComboBox type = new JComboBox();
public JPanel fontPanel = new JPanel();
public SetFont(){
innit();
}
public void innit(){
fontPanel.setLayout(new FlowLayout());//设置默认布局
fontPanel.add(size);
fontPanel.add(type);
fontPanel.add(ok);
fontPanel.add(cancel);
size.addItem("选择字体");
size.addItem("12");
size.addItem("20");
size.addItem("30");
size.addItem("50");
type.addItem("选择字形");
type.addItem("宋体");
type.addItem("黑体");
type.addItem("微软雅黑");
this.setSize(400,100);
this.setLocation(0,100);
this.setContentPane(fontPanel);
this.setVisible(true);
this.setResizable(false);//设置窗体大小不可改
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
HelpYou hy = new HelpYou();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-12-11
展开全部
事件监听不会加?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询