我必须在下面的 GUI 中添加一些要点。以下是我到目前为止所做的代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*;
public class PoiS extends JFrame implements ActionListener{
JPanel p;
JButton bplus, bminus;
double factor = 0.5;
JPanel titlePanel;
JLabel redLabel;
PoiS(){
p = new DrawingPanel();
/*
buttonMinus = new JButton("Zoom In");
buttonPlus = new JButton(" Zoom Out ");
buttonMinus.addActionListener(this);
buttonPlus.addActionListener(this);
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(buttonMinus);
box.add(Box.createHorizontalStrut(20));
box.add(buttonPlus);
box.add(Box.createHorizontalGlue());
add(box, BorderLayout.SOUTH);
*/
//Here i was creating a new Panel To enter to my Original panel P
titlePanel = new JPanel();
titlePanel.setLayout(null);
titlePanel.setLocation(10, 0);
titlePanel.setSize(250, 30);
p.add(titlePanel);
/*
redLabel = new JLabel("Red Team");
redLabel.setLocation(0, 0);
redLabel.setSize(120, 30);
redLabel.setHorizontalAlignment(0);
redLabel.setForeground(Color.red);
titlePanel.add(redLabel);*/
bplus = new JButton("+");
bminus = new JButton("-");
bplus.addActionListener(this);
bminus.addActionListener(this);
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(bminus);
box.add(Box.createHorizontalStrut(20));
box.add(bplus);
box.add(Box.createHorizontalGlue());
add(box, BorderLayout.SOUTH);
//add(new JLabel("\n"),BorderLayout.NORTH);
//add(new JLabel(" subash "),BorderLayout.EAST);
//add(new JLabel(" kafley "),BorderLayout.WEST);
JLabel lbl = new JLabel();
lbl.setText("Hotel1");
//lbl.setText("hotel1");
lbl.setLocation(20,20);
lbl.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == bplus) {
factor += 0.1;
}
else {
factor -= 0.1;
}
bminus.setEnabled(factor > 0.1);
bplus.setEnabled(factor < 4.0);
p.repaint();
JLabel redLabel = new JLabel("Red Team");
redLabel.setLocation(0, 0);
redLabel.setSize(120, 30);
redLabel.setHorizontalAlignment(0);
redLabel.setForeground(Color.red);
//redLabel.setVisible(true);
redLabel.add(p);
//redLabel.setVisible(true);
}
public static void main(String []arg) {
/* File file = new i was trying to get my x axis and y axis from the txt file File("/Users/kafley/Documents/Studymaterial/ITC313/Assignment1/src/Points.txt");
FileReader fr = new FileReader(file);
BufferedReader in = new BufferedReader(fr);
String line;
while ((line = in.readLine()) != null){
String[] record = line.split(",\\s*");
for(int i=0;i<record.length; i++){
System.out.print(record[i]);*/
/*String line;
try {
BufferedReader reader = new BufferedReader(new FileReader("/Users/kafley/Documents/Studymaterial/ITC313/Assignment1/src/Points.txt"));
while((line = reader.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}*/
PoiS print = new PoiS();
JLabel lbl = new JLabel();
lbl.setText("Hotel1");
lbl.setLocation(20, 20);
lbl.setVisible(true);
}
}
and another class
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
/**
*
* @author kafley
*/
class DrawingPanel extends JPanel {
double squareSize[] = {100.0, 50.0};
double factor = 0.5;
DrawingPanel() {
super(null);
setBorder(BorderFactory.createLineBorder(Color.RED));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension dim = this.getSize();
int width = dim.width - 10;
int height = dim.height - 10;
for(int i = 0; i < squareSize.length; ++i) {
int squareWidth = (int) (squareSize[i] * factor);
int x = (width - squareWidth) / 2;
int y = (height - squareWidth) / 2;
g.drawRect(x, y, squareWidth, squareWidth);
}
}
}
只是忽略那些评论部分作为我的 Netbeans 的复制粘贴
我不想删除,因为我认为我将来可能需要它。我只是想让你帮我在这个框架中绘制另一个 JPanel。任何帮助将不胜感激