『壹』 java panel中添加panel
設置布局管理器
如果你想按順序可以使用FlowLayout和GridLayout
當然也可以按照你想的精確位版置,調用setBounds()方法權來設置panel的位置和大小
不過這種要將布局管理設成null
例如panel.setLayout(new FlowLayout());
例如panel.setLayout(null);
『貳』 java中的panel 問題
不知道lz是否是想實現這樣的效果??
下面是修改之後的程序:
import java.awt.*;
import java.awt.event.*;
public class GraphicsDDA extends Frame implements ItemListener, ActionListener {
private Label label1;
private TextField startX;
private Label label2;
private TextField startY;
private Label label3;
private TextField endX;
private Label label4;
private TextField endY;
private Choice colorChoice;
private Button start;
private Panel panel;
public GraphicsDDA() {
super("DDA");
label1 = new Label("起點X");
startX = new TextField(5);
label2 = new Label("起點Y");
startY = new TextField(5);
label3 = new Label("終點X");
endX = new TextField(5);
label4 = new Label("終點Y");
endY = new TextField(5);
colorChoice = new Choice();
colorChoice.add("BLACK");
colorChoice.add("BLUE");
colorChoice.add("GREEN");
colorChoice.add("CYAN");
colorChoice.add("RED");
colorChoice.add("MAGENTA");
colorChoice.add("LIGHTGRAY");
colorChoice.add("DARKGRAY");
start = new Button("開始");
panel = new Panel();
panel.add(label1);
panel.add(startX);
panel.add(label2);
panel.add(startY);
panel.add(label3);
panel.add(endX);
panel.add(label4);
panel.add(endY);
panel.add(colorChoice);
panel.add(start);
add("Center", panel);
// panel.setSize(100, 100);
// panel.setLocation(100, 100);
// panel.setBackground(Color.white);
// this.setLayout(new BorderLayout());
this.setSize(800, 600);
//this.setBackground(Color.lightGray);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent e) {
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
new GraphicsDDA();
}
}
『叄』 java中panel怎麼設置位置
(1)
『肆』 java中panel 面板的問題
面板實際上就是一個容器,之後可以任意在裡面添加(add)或者刪除(remove)內容。
例如單擊某一個組件就移除這個組件,並且添加另外的組件,下面是一個具體的例子:
import java.awt.FlowLayout;
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 WinTest3
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLayout(new FlowLayout());
JPanel panel = new JPanel();
JButton button = new JButton("change");
panel.add(button);
JTextField f = new JTextField(20);
ActionListener listener = new ChangeListener(button,panel,f);
button.addActionListener(listener);//注冊監聽器
frame.add(panel);
frame.setVisible(true);
}
}
/*監聽器,當單擊按鈕時,移除button按鈕,加入text文本框*/
class ChangeListener implements ActionListener
{
JButton button;
JPanel panel;
JTextField text;
public ChangeListener(JButton button, JPanel panel, JTextField text)
{
super();
this.button = button;
this.panel = panel;
this.text = text;
}
@Override
public void actionPerformed(ActionEvent e)
{
if("change".equals(e.getActionCommand()))
{
panel.remove(button);
panel.add(text);
panel.updateUI();
panel.repaint();
}
}
}
備註:需要特別注意的是移除和添加組件之後,記得重畫組件。
『伍』 java中的JPanel怎麼用
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
JPanelpanel=newJPanel();
JTextFieldjtf3=newJTextField("指定文本內容");
jtf3.setFont(newFont("諧體",Font.BOLD|Font.ITALIC,16));
//設置文本的水平對齊方式
jtf3.setHorizontalAlignment(JTextField.CENTER);
panel.add(jtf3);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
『陸』 關於java中panel的問題。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
class Test {
public static void main(String[] args) {
System.getProperties().list(System.out);
System.out.println(System.getProperty("user.home") + "/Desktop");
JLabel label = new JLabel();
label.setText("123456");
Dimension size = label.getPreferredSize();
BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_BGR);
// label.paint(image.createGraphics()); /*這個方法只有label顯示的時候才能用,而且圖片樣子和label顯示的是一模一樣的*/
Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fill(new Rectangle(0,0,size.width,size.height));
g.setColor(Color.BLACK);
g.drawString(label.getText(), 0, 10);
try {
ImageIO.write(image, "jpg", new File("D:\\1.jpg"));
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
看看把,參考一下!
『柒』 Java程序頁面中如何在panel里添加panel!
首先指出代碼中一些不好的用法,比如:setLayout(null),
在我的java 1.4.2的環境中編譯會出錯。
實際上,應該使用this.getContentPane().setLayout();
這是JFrame和Frame不同的地方。
另外,在swing裡面,也不推薦使用setVisible()來顯示窗口,而應該使用show();
在我這里運行正常的代碼如下:
/*
* Created on 2005-3-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.ubi.config.demo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class aaa extends JFrame
{
aaa()
{
P1 p1=new P1();
P2 p2=new P2();
this.getContentPane().setLayout(null);
setBounds(200,200,500,300);
p1.setBounds(0,0,400,300);
getContentPane().add(p1);
p2.setBounds(400,0,100,300);
getContentPane().add(p2);
}
public static void main(String[] args)
{
aaa test=new aaa();
test.show();
}
//================================================
class P1 extends JPanel
{
P1()
{
btStart=new JButton( "111 ");
add(btStart);
}
private JButton btStart;
}
class P2 extends JPanel
{
P2()
{
btStart=new JButton( "222 ");
add(btStart);
}
private JButton btStart;
}
}//:-)
『捌』 java中panel對象裡麵包含什麼方法,可以通過什麼方式可以查看到
可以,你在Frame的構造函數中將controlPanel的實例添加到下面,將mainPanel的實例添加到中間,代碼大致如下:
先在controlPanel的構造函數中添加一個mainPanel類的參數,在其實例化時傳進去,注意要先實例化mainPanel,如:
MainPanel mainPanel = new MainPanel();
ControlPanel controlPanel = new ControlPanel(mainPanel);
然後在Frame框架的構造函數中添加
this.add(controlPanel, Layout.SOUNTH);
this.add(mainPanel, Layout.CENTER);
這時controlPanel 對象中包含了mainPanel 對象的引用,就可以在其方法中控制mainPanel 對象了,但要注意mainPanel 對象的相關屬性或方法必須是public的
『玖』 Java 中的panel()面板有什麼用,怎麼用。
//Jpanel和panel都為中間層容器,可顯示文字、圖像、繪制圖形,主要功能是
在GUI中組織其他組件。但無邊框,丌能做獨立窗口。以Jpanel為例,給一個曾經的實例
//MyPanel.java
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class MyPanel extends JPanel {
private Circle circle[];
public MyPanel() {
setBackground(Color.black); // 面板背景色
setPreferredSize(new Dimension(600,450)); // 面板大小
int numCircle=new Random().nextInt(6)+5;
//int numCircle=circle.length;
circle=new Circle[numCircle];
for(int i=0;i<numCircle;i++){ // 創建圓的實例
Color color=new Color(new Random().nextInt(256),new Random().nextInt(256),new Random().nextInt(256));
circle[i]=new Circle(new Random().nextInt(50)+10,color,new Random().nextInt(420)+61,new Random().nextInt(320)+61);
}
}
/* 每個JPanel對象都有painComponent方法,繪制添加在容器上的組件, 在
* 組件首次顯示或重新顯示時被自動調用。
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 調用父類方法, 清空畫布,繪制背景色
setFont(new Font ("Times New Roman",Font.BOLD,20)); // 設置字體
g.setColor(Color.pink);
g.drawString("Hello World!",250,200);
for(int i=0;i<circle.length;i++){
circle[i].draw(g);
}
}
}
//Circle.java
import java.awt.*;
/**此類定義了繪制圓的基本方法
*@author JF
*@version 1.0
*/
public class Circle {
// 直徑和圓外接正方形左上角坐標
private int diameter,x,y;
// 指定圓的大小和位置
private Color color; // 顏色
public Circle(){
}
/**構造函數,設置圓類的屬性
*@param int diameter,Color color,int x,int y
*@return none
*/
Circle(int diameter,Color color,int x,int y ){
this.diameter=diameter;
this.color=color;
this.x=x;
this.y=y;
}
/**填充圓類
*@param Graphics
*@return none
*/
public void draw(Graphics g){
g.setColor(color);
g.fillOval(x,y,diameter,diameter);
}
}
//MyCircle.java
import javax.swing.*;
public class MyCircle{
public static void main(String[] args){
JFrame f=new JFrame("Circle");
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new MyPanel());
f.pack();
}
}
『拾』 java的Jpanel方法
首先remove(int) 不是JPanel自己的方法,是它從Container類繼承而來的方法
Container顧名思義,是組件的容器,它可以放許多組件。而remove(int)的參數顯然不是組件的id,而是它在容器中的索引(如同數組的下標)
你要刪除一個組件,可以使用 Container的remove(Component)方法,因為這個Component很容易通過事件獲得,而它的索引則需要做執行一步才能獲得。