请给出一个画图程序,要求如下:java 语言,建立窗体JFrame,里面添加面板JPanel,在面板上绘制一个矩形。 30
对于矩形位置是固定的,但是宽度和高度由形参给出,且形参是不固定的。所以该面板还要能够随时刷新以便更新该矩形的宽度和高度。给的程序好的再加20分...
对于矩形位置是固定的,但是宽度和高度由形参给出,且形参是不固定的。所以该面板还要能够随时刷新以便更新该矩形的宽度和高度。给的程序好的再加20分
展开
1个回答
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test
{
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame();
final RectanglePanel panel = new RectanglePanel();
frame.add(panel, BorderLayout.CENTER);
JPanel paramPanel = new JPanel();
paramPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
final JTextField widthField = new JTextField(5);
final JTextField heightField = new JTextField(5);
paramPanel.add(widthField);
paramPanel.add(heightField);
JButton button = new JButton("Draw");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int width = Integer.parseInt(widthField.getText());
int height = Integer.parseInt(heightField.getText());
panel.setRectangleSize(width, height);
panel.repaint();
} catch (Exception ex) {}
}
});
paramPanel.add(button);
frame.add(paramPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
}
class RectanglePanel extends JPanel {
int width = 0, height = 0;
void setRectangleSize(int width, int height) {
this.width = width;
this.height = height;
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(1, 1, width, height);
}
}
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test
{
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame();
final RectanglePanel panel = new RectanglePanel();
frame.add(panel, BorderLayout.CENTER);
JPanel paramPanel = new JPanel();
paramPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
final JTextField widthField = new JTextField(5);
final JTextField heightField = new JTextField(5);
paramPanel.add(widthField);
paramPanel.add(heightField);
JButton button = new JButton("Draw");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int width = Integer.parseInt(widthField.getText());
int height = Integer.parseInt(heightField.getText());
panel.setRectangleSize(width, height);
panel.repaint();
} catch (Exception ex) {}
}
});
paramPanel.add(button);
frame.add(paramPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
}
class RectanglePanel extends JPanel {
int width = 0, height = 0;
void setRectangleSize(int width, int height) {
this.width = width;
this.height = height;
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(1, 1, width, height);
}
}
追问
请问这样怎么办?有未知个panel需要添加到固定大小的jscrollpanel上,每个panel上画一个矩形,panel大小也是固定的,需要布局是每行2个panel,下面增加,超出部分jscrollpanel自动滚动,若其中某个panel被remove了后面的panel自动前移,请给出程序,谢了,给的好加100分。我说到做到。
追答
@__@
你这还不如新开一个问题... 一方面有更多人可以看到,另外一方面我看到追问的回答字数上限是1000字,我觉得容纳这个代码未必够
先这样吧,等会儿写写看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询