kanonmoon kanonmoon
关注数: 25 粉丝数: 92 发帖数: 1,082 关注贴吧数: 17
比赛!谁可以把以下的程序用最少的代码执行出来 这个只是一个简单checkBox的程序 构思并不复杂 但重复的代码超级多 我都不想修了特此在这里召集高手们比赛 看看谁可以把以下的程序用最少的代码写出来随便给我们这些新手学习一下 import java.awt.EventQueue; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Practice9 extends JFrame { String[] cbBox = new String[4];// button的数目 String[] cbCheck = { "", "", "" };// checkBox的数目 private JPanel contentPane; private JCheckBox checkBox1; private JCheckBox checkBox2; private JCheckBox checkBox3; private JButton button1; private JButton button2; private JButton button3; private JButton button4; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Practice9 frame = new Practice9(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Practice9() { 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); button1 = new JButton("红蓝"); button1.setBounds(131, 40, 87, 23); cbBox[0] = "#r#b"; contentPane.add(button1); button2 = new JButton("红绿"); button2.setBounds(267, 40, 87, 23); cbBox[1] = "#r#g"; contentPane.add(button2); button3 = new JButton("蓝"); button3.setBounds(131, 94, 87, 23); cbBox[2] = "#b"; contentPane.add(button3); button4 = new JButton("绿"); button4.setBounds(267, 94, 87, 23); cbBox[3] = "#g"; contentPane.add(button4); checkBox1 = new JCheckBox("红"); checkBox1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
求scrollBar的使用用法 import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JScrollBar; public class Test01 extends JFrame { private JPanel contentPane; private JButton button1; private JPanel panel; private JScrollBar scrollBar; private JButton button2; private JButton button3; private JButton button4; private JButton button5; private JButton button6; private JButton button7; private JButton button8; private JButton button9; private JButton button10; private JButton button11; private JButton button12; private JScrollBar scrollBar_1; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Test01 frame = new Test01(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Test01() { 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); panel = new JPanel(); panel.setBounds(118, 43, 195, 165); contentPane.add(panel); panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); button1 = new JButton("1"); button1.setPreferredSize(new Dimension(50, 50)); panel.add(button1); button2 = new JButton("2"); button2.setPreferredSize(new Dimension(50, 50)); panel.add(button2); button3 = new JButton("3"); button3.setPreferredSize(new Dimension(50, 50)); panel.add(button3); button4 = new JButton("4"); button4.setPreferredSize(new Dimension(50, 50)); panel.add(button4); button5 = new JButton("5"); button5.setPreferredSize(new Dimension(50, 50)); panel.add(button5); button6 = new JButton("6"); button6.setPreferredSize(new Dimension(50, 50)); panel.add(button6); button7 = new JButton("7"); button7.setPreferredSize(new Dimension(50, 50)); panel.add(button7); button8 = new JButton("8"); button8.setPreferredSize(new Dimension(50, 50)); panel.add(button8); button9 = new JButton("9"); button9.setPreferredSize(new Dimension(50, 50)); panel.add(button9); button10 = new JButton("10"); button10.setPreferredSize(new Dimension(50, 50)); panel.add(button10); button11 = new JButton("11"); button11.setPreferredSize(new Dimension(50, 50)); panel.add(button11); button12 = new JButton("12"); button12.setPreferredSize(new Dimension(50, 50)); panel.add(button12); scrollBar_1 = new JScrollBar(); panel.add(scrollBar_1); } } 求里面最右面加上一个scrollBar 之后可以上下拉动 求高手教教我
程式没有反应 求帮助 import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Practice8 extends JFrame { private JPanel contentPane; private final ButtonGroup buttonGroup = new ButtonGroup(); boolean twMode = true; boolean naMode = false; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Practice8 frame = new Practice8(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Practice8() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 632, 423); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu mnFile = new JMenu(naMode == true ? "File" : "档案"); menuBar.add(mnFile); JMenu mnNatwPath = new JMenu(naMode == true ? "Path" : "路径"); menuBar.add(mnNatwPath); JMenu mnLauguage = new JMenu(naMode == true ? "Language" : "语言"); menuBar.add(mnLauguage); JRadioButton rdbtnTw = new JRadioButton("TW"); rdbtnTw.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { naMode = false; twMode = true; System.out.println("TW Mode On"); } }); rdbtnTw.setSelected(true); buttonGroup.add(rdbtnTw); mnLauguage.add(rdbtnTw); JRadioButton rdbtnNa = new JRadioButton("NA"); rdbtnNa.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { naMode = true; twMode = false; System.out.println("NA Mode On"); } }); buttonGroup.add(rdbtnNa); mnLauguage.add(rdbtnNa); JMenu mnHelp = new JMenu(naMode == true ? "Help" : "帮助"); menuBar.add(mnHelp); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); } } 想按语言的时候会自动刷新程式的所有语言 但是都没有反应 求高手找一下错在那里
scrollBar的问题 求高手帮助除错 public class Practice8 implements AdjustmentListener { JScrollBar scrollBar1; JScrollBar scrollBar2; JPanel panel1; JLabel label2 = new JLabel("刻度:", JLabel.CENTER); private JButton button1; private JButton button2; public static void main(String[] args) { new Practice8(); } public Practice8() { JFrame f = new JFrame("JScrollBarDemo"); Container contentPane = f.getContentPane(); JLabel label1 = new JLabel(); label1.setBounds(88, 5, -1, -1); panel1 = new JPanel(); panel1.setLayout(null); panel1.add(label1); /* * 产生一个垂直滚动轴,默认滚动轴位置在10刻度的地方,extent值设10,minimum值为0, * maximan值为100,因此滚动轴一开始在刻度10的位置上,可滚动的区域大小为100-10-0=90刻度,滚动范围在0~90中。 */ scrollBar1 = new JScrollBar(JScrollBar.VERTICAL, 0, 30, 0, 100); scrollBar1.setUnitIncrement(1);// 设置拖曳滚动轴时,滚动轴刻度一次的变化量。 scrollBar1.setBlockIncrement(10);// 设置当鼠标在滚动轴列上按一下是,滚动轴一次所跳的区块大小 scrollBar1.addAdjustmentListener(this); scrollBar2 = new JScrollBar();// 建立一个空的JScrollBar scrollBar2.setOrientation(JScrollBar.HORIZONTAL);// 设置滚动轴方向为水平方向 scrollBar2.setValue(0);// 设置默认滚动轴位置在0刻度的地方。 scrollBar2.setVisibleAmount(20);// extent值设为20 scrollBar2.setMinimum(10);// minmum值设为10 scrollBar2.setMaximum(60);// maximan值设为60,因为minmum值设为10,可滚动的区域大小为60-20-10=30 // 个刻度,滚动范围在10~40中。 scrollBar2.setBlockIncrement(5);// 当鼠标在滚动轴列上按一下时,滚动轴一次所跳的区块大小为5个刻度 scrollBar2.addAdjustmentListener(this); contentPane.add(panel1, BorderLayout.CENTER); button1 = new JButton("1"); button1.setBounds(44, 24, 87, 23); panel1.add(button1); button2 = new JButton("2"); button2.setBounds(44, 80, 87, 23); panel1.add(button2); contentPane.add(scrollBar1, BorderLayout.EAST); contentPane.add(scrollBar2, BorderLayout.SOUTH); contentPane.add(label2, BorderLayout.NORTH); f.setSize(new Dimension(200, 200)); f.show(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } // 实现adjustmentValueChanged方法。当用户改变转轴位置时,会将目前的滚动轴刻度写在labe2上。 public void adjustmentValueChanged(AdjustmentEvent e) { if ((JScrollBar) e.getSource() == scrollBar1) label2.setText("垂直刻度" + e.getValue());// e.getValue()所得的值与scrollBar1.getValue()所得的值一样。 button1.setBounds(44, (80 + scrollBar1.getValue()), 87, 23); button2.setBounds(44, (80 + scrollBar1.getValue()), 87, 23); if ((JScrollBar) e.getSource() == scrollBar2) label2.setText("水平刻度" + e.getValue()); button1.setBounds((44 + scrollBar2.getValue()), 80, 87, 23); button2.setBounds((44 + scrollBar2.getValue()), 80, 87, 23); } } 实在不知道错在那里 按键2突然会消失掉 而且有一个bar还不能动 求高手帮忙
1 下一页