Java 添加额外的Jpabnel
我必须在下面的GUI中添加一些要点。以下是我到目前为止所做的代码Java 添加额外的Jpabnel,java,user-interface,jpanel,Java,User Interface,Jpanel,我必须在下面的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.FileNotFoundExcep
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);
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入javax.swing.BorderFactory;
导入javax.swing.JPanel;
/**
*
*@作者卡弗利
*/
类DrawingPanel扩展了JPanel{
双平方尺寸[]={100.0,50.0};
双因子=0.5;
绘图面板(){
超级(空);
setboorder(BorderFactory.createLineBorder(Color.RED));
}
@凌驾
公共组件(图形g){
超级组件(g);
Dimension dim=this.getSize();
内部宽度=尺寸宽度-10;
内部高度=尺寸高度-10;
对于(int i=0;i
从我的Netbeans复制粘贴时忽略这些注释部分
我不想删除,因为我认为我将来可能需要它。我只是想让你帮我在这个画面里画出另一个JPanel。任何帮助都将不胜感激我不确定。。。但是这样可以吗
redLabel.add(p);// <- redLabel is JLabel,and p is JPanel...?
redLabel.add(p)//
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);
}
}
}
redLabel.add(p);// <- redLabel is JLabel,and p is JPanel...?