java 怎么在Canvas添加组件_java – 在Swing中指定Canvas的位置

“Swing程序应该覆盖paintComponent()而不是覆盖paint().” –

Painting in AWT and Swing: The Paint Methods. JPanel或JComponent是常见的选择,如建议的

here.您可以使用合适的

layout控制放置.

附录:这与Canvas有什么关系?

java.awt.Canvas类是一个AWT组件;而是使用Swing组件javax.swing.JPanel.这是您的程序的一个变体,只是选择一个随机颜色,但它可能会让您知道如何解决您的其他属性.有一个相关的例子here.

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class MainCode {

public static void main(String args[]) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

MainView fc = new MainView();

}

});

}

private static class MainView implements ActionListener {

private JFrame f = new JFrame();

private JButton colorButton = new JButton("Color");

private JButton quitButton = new JButton("Quit");

private JButton infoButton = new JButton("Info");

private JLabel x_loc = new JLabel("X:");

private JLabel y_loc = new JLabel("Y:");

private JLabel w_label = new JLabel("Width:");

private JLabel h_label = new JLabel("Height:");

private JTextField x_loc_box = new JTextField("0");

private JTextField y_loc_box = new JTextField("0");

private JTextField w_loc_box = new JTextField("100");

private JTextField h_loc_box = new JTextField("100");

private JOptionPane info1 = new JOptionPane();

private JPanel panel1 = new JPanel();

private JPanel panel2 = new JPanel();

private GraphicsClass graphicsClass = new GraphicsClass();

public MainView() {

panel1.add(x_loc);

panel1.add(x_loc_box);

panel1.add(y_loc);

panel1.add(y_loc_box);

panel1.add(w_label);

panel1.add(w_loc_box);

panel1.add(h_label);

panel1.add(h_loc_box);

colorButton.addActionListener(this);

quitButton.addActionListener(this);

infoButton.addActionListener(this);

panel2.add(colorButton);

panel2.add(quitButton);

panel2.add(infoButton);

f.add(panel1, BorderLayout.NORTH);

f.add(graphicsClass, BorderLayout.CENTER);

f.add(panel2, BorderLayout.SOUTH);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setTitle("Graphics Toolbox v2");

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource() == infoButton) {

JOptionPane.showMessageDialog(f, "hahahahahaha");

} else if (e.getSource() == quitButton) {

System.exit(0);

} else if (e.getSource() == colorButton) {

graphicsClass.randomColor();

graphicsClass.repaint();

}

}

}

private static class GraphicsClass extends JPanel {

private static final int SIZE = 128;

private static final Random r = new Random();

private Color color = Color.green;

@Override

public Dimension getPreferredSize() {

return new Dimension(SIZE, SIZE);

}

public void randomColor() {

this.color = new Color(r.nextInt());

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(color);

int w = getWidth();

int h = getHeight();

g.fillArc(0, h / 4, w, h, 45, 90);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值