java setforeground_Java JPanel.setForeground方法代码示例

import javax.swing.JPanel; //导入方法依赖的package包/类

/**

* Creates an instance of this class.

*/

protected GASettings () {

setTitle("Genetic Algorithm Settings");

setBounds(50,50,520,520);

centerPanel = new JPanel(new FlowLayout());

JPanel southPanel = new JPanel(new FlowLayout());

maxGenerationsLabel = new JLabel("Max Generations");

maxGenerationsField = new JTextField((new Integer(TSProblemModel.DEFAULT_GA_MAX_GENERATIONS)).toString(), 5);

popSizeLabel = new JLabel("Population Size");

popSizeField = new JTextField((new Integer(TSProblemModel.DEFAULT_GA_POP_SIZE)).toString(), 5);

mutationRateLabel = new JLabel("Mutation Rate");

mutationRateField = new JTextField((new Double(TSProblemModel.DEFAULT_MUTATION_RATE)).toString(), 5);

orderedCrossoverBox = new JCheckBox("Ordered Crossover", TSProblemModel.DEFAULT_ORDERED_CROSSOVER);

cyclicCrossoverBox = new JCheckBox("Cyclic Crossover", TSProblemModel.DEFAULT_CYCLIC_CROSSOVER);

pmxCrossoverBox = new JCheckBox("PMX Crossover", TSProblemModel.DEFAULT_PMX_CROSSOVER);

simulatedAnnealingBox = new JCheckBox("Simulated Annealing", TSProblemModel.DEFAULT_ANNEAL);

inverOverBox = new JCheckBox("Inver Over", TSProblemModel.DEFAULT_INVER_OVER);

centerPanel.add(maxGenerationsLabel);

centerPanel.add(maxGenerationsField);

centerPanel.add(popSizeLabel);

centerPanel.add(popSizeField);

centerPanel.add(mutationRateLabel);

centerPanel.add(mutationRateField);

centerPanel.add(orderedCrossoverBox);

centerPanel.add(cyclicCrossoverBox);

centerPanel.add(pmxCrossoverBox);

centerPanel.add(inverOverBox);

centerPanel.add(simulatedAnnealingBox);

centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

centerPanel.setForeground(Color.getColor("202,198,202"));

// Create buttons and buttons panel

JPanel buttonsPanel = new JPanel(new FlowLayout());

buttonsPanel.add(new JButton(RESTORE_DEFAULT_ACTION));

buttonsPanel.add(new JButton(OK_ACTION));

buttonsPanel.add(new JButton(CANCEL_ACTION));

// Add buttons panel in South Panel

southPanel.add(buttonsPanel,BorderLayout.EAST);

// Add panels in Main Panel

this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().add(southPanel,BorderLayout.SOUTH);

this.getContentPane().add(centerPanel,BorderLayout.CENTER);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Java 画图板示例: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimplePaint extends JFrame implements ActionListener, MouseListener, MouseMotionListener { private JButton clearBtn, blackBtn, blueBtn, greenBtn, redBtn; private JLabel mousePos; private JPanel drawPanel; private int prevX, prevY, currX, currY; public SimplePaint() { super("Simple Paint"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建按钮 clearBtn = new JButton("Clear"); blackBtn = new JButton("Black"); blueBtn = new JButton("Blue"); greenBtn = new JButton("Green"); redBtn = new JButton("Red"); // 设置按钮监听器 clearBtn.addActionListener(this); blackBtn.addActionListener(this); blueBtn.addActionListener(this); greenBtn.addActionListener(this); redBtn.addActionListener(this); // 创建面板 JPanel btnPanel = new JPanel(); btnPanel.add(clearBtn); btnPanel.add(blackBtn); btnPanel.add(blueBtn); btnPanel.add(greenBtn); btnPanel.add(redBtn); mousePos = new JLabel("Mouse position: "); drawPanel = new JPanel(); drawPanel.setBackground(Color.white); drawPanel.addMouseListener(this); drawPanel.addMouseMotionListener(this); add(btnPanel, BorderLayout.NORTH); add(mousePos, BorderLayout.SOUTH); add(drawPanel, BorderLayout.CENTER); setSize(600, 400); setVisible(true); } // 按钮事件处理 public void actionPerformed(ActionEvent e) { if (e.getSource() == clearBtn) { Graphics g = drawPanel.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight()); } else if (e.getSource() == blackBtn) { drawPanel.setForeground(Color.black); } else if (e.getSource() == blueBtn) { drawPanel.setForeground(Color.blue); } else if (e.getSource() == greenBtn) { drawPanel.setForeground(Color.green); } else if (e.getSource() == redBtn) { drawPanel.setForeground(Color.red); } } // 鼠标事件处理 public void mousePressed(MouseEvent e) { prevX = e.getX(); prevY = e.getY(); } public void mouseDragged(MouseEvent e) { currX = e.getX(); currY = e.getY(); Graphics g = drawPanel.getGraphics(); g.drawLine(prevX, prevY, currX, currY); prevX = currX; prevY = currY; } public void mouseClicked(MouseEvent e) { mousePos.setText("Mouse position: " + e.getX() + ", " + e.getY()); } public void mouseMoved(MouseEvent e) { mousePos.setText("Mouse position: " + e.getX() + ", " + e.getY()); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public static void main(String[] args) { new SimplePaint(); } } ``` 这个简单的画图板示例包含了颜色选择、清除以及鼠标事件处理等基本功能。你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值