我想在运行时添加、移动和设置组件的属性(字体和旋转) 。
换句话说,我需要添加一个按钮、一个标签、使用鼠标拖动它们并在运行时设置组件的属性。是否有任何示例 Java swing 或类似的?
这是我的代码 :
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.File;
import java.sql.SQLException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class prova extends JFrame implements MouseMotionListener,MouseListener{
//*************** INIZIALIZZAZIONE VARIABILI ***************************
Checkbox CB=new Checkbox("Label",true);
Button BP=new Button("BUTTON_1");
Button B=new Button("OK");
JComboBox combo1=new JComboBox();
JMenuBar menuBar;
int app=0;
static int anglePeso=0;
static int angleTara=0;
ImageIcon ic;
Border border = LineBorder.createBlackLineBorder();
static int pesoX=184;
static int pesoY=91;
static boolean taraVisible=false;
static boolean pesoVisible=false;
static boolean logo1Visible=false;
static boolean BarCodeVisible=false;
static String font="";
static String rot="";
Label societa=new Label ("Società");
Label importo=new Label ("Importo");
Label nome=new Label("Nome Prodotto");
static int logo1X=250;
static int logo1Y=90;
static Component C;
static int label1X=183;
static int label1Y=91;
static int taraX=100;
//TextField TF=new TextField(10);
static Panel P=new Panel();
//List TA=new List();
boolean b1=false;
int c1=0;int c2=0;
static JLabel logo2=new JLabel("Logo2");
static int aa=0;
static int importoX=0;
static int importoY=0;
public static boolean isBarCodeVisible() {
return BarCodeVisible;
}
public static void setBarCodeVisible(boolean BarCodeVisible) {
prova.BarCodeVisible = BarCodeVisible;
}
public static Component getC() {
return C;
}
public static void setC(Component C) {
prova.C = C;
}
public static int getAnglePeso() {
return anglePeso;
}
public static void setAnglePeso(int anglePeso) {
prova.anglePeso = anglePeso;
}
static ImageIcon immLogo1;
public JLabel getBarcode() {
return barcode;
}
public void setBarcode(JLabel barcode) {
this.barcode = barcode;
}
public static ImageIcon getImmLogo1() {
return immLogo1;
}
public static void setImmLogo1(ImageIcon immLogo1) {
prova.immLogo1 = immLogo1;
}
public static void setFont(String font) {
prova.font = font;
}
public static String getRot() {
return rot;
}
public static void setRot(String rot) {
prova.rot = rot;
}
public static boolean isPesoVisible() {
return pesoVisible;
}
public static void setPesoVisible(boolean pesoVisible) {
prova.pesoVisible = pesoVisible;
}
public static boolean isTaraVisible() {
return taraVisible;
}
public static void setTaraVisible(boolean taraVisible) {
prova.taraVisible = taraVisible;
}
static JLabel label1=new JLabel("Peso"){
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform aT = g2.getTransform();
Shape oldshape = g2.getClip();
double x = getWidth()/2.0;
double y = getHeight()/2.0;
aT.rotate(Math.toRadians(anglePeso), x, y);
// g2.transform(aT);
g2.setTransform(aT);
g2.setClip(oldshape);
super.paintComponent(g);
}
};
Label data=new Label("Data");
static JLabel tara=new JLabel("Tara"){
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform aT = g2.getTransform();
Shape oldshape = g2.getClip();
double x = getWidth()/2.0;
double y = getHeight()/2.0;
aT.rotate(Math.toRadians(angleTara), x, y);
// g2.transform(aT);
g2.setTransform(aT);
g2.setClip(oldshape);
super.paintComponent(g);
}
};
static JLabel logo1=new JLabel("Logo1");
public static JLabel getTara() {
return tara;
}
public static void setTara(JLabel tara) {
prova.tara = tara;
}
static JLabel barcode=new JLabel ("Bar Code");/*{
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform aT = g2.getTransform();
Shape oldshape = g2.getClip();
double x = getWidth()/2.0;
double y = getHeight()/2.0;
aT.rotate(Math.toRadians(angle), x, y);
// g2.transform(aT);
g2.setTransform(aT);
g2.setClip(oldshape);
super.paintComponent(g);
}
};
*/
public static JLabel getLogo1() {
return logo1;
}
public static void setLogo1(JLabel logo1) {
prova.logo1 = logo1;
}
public static boolean isLogo1Visible() {
return logo1Visible;
}
public static void setLogo1Visible(boolean logo1Visible) {
prova.logo1Visible = logo1Visible;
}
public static int getLogo1X() {
return logo1X;
}
public static void setLogo1X(int logo1X) {
prova.logo1X = logo1X;
}
public static int getLogo1Y() {
return logo1Y;
}
public static void setLogo1Y(int logo1Y) {
prova.logo1Y = logo1Y;
}
public static int getTaraX() {
return taraX;
}
public static void setTaraX(int taraX) {
prova.taraX = taraX;
}
public static int getTaraY() {
return taraY;
}
public static void setTaraY(int taraY) {
prova.taraY = taraY;
}
static int taraY=90;
public int getLabel1X() {
return label1X;
}
public void setLabel1X(int label1X) {
this.label1X = label1X;
}
public int getLabel1Y() {
return label1Y;
}
public void setLabel1Y(int label1Y) {
this.label1Y = label1Y;
}
public prova(){
}
public prova(String titlu)
{
super(titlu);
}
void init(int b,int h, String [] par) throws ClassNotFoundException, SQLException
{
setLayout(null);
System.out.println("****************COORINATE PESO: "+label1X+" "+label1Y);
menuBar = new JMenuBar();
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Save");
JMenuItem editItem1 = new JMenuItem("Cut");
JMenuItem editItem2 = new JMenuItem("Copy");
editItem2.add(new JSeparator());
JMenuItem editItem3 = new JMenuItem("Paste");
JMenuItem editItem4 = new JMenuItem("Insert");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
setJMenuBar(menubar);
//setSize(400,400);
//setVisible(true);
fileItem4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Cliccato Save ");
punti();
setVisible(false);
}
});
InserimentoDB is=new InserimentoDB();
Image im=is.getImage("select immagine from etichette where id= 75");
ic=new ImageIcon(im);
P.setBackground(Color.red);
String[] mm=new String [4];
mm[0]="0";
mm[1]="90";
mm[2]="180";
mm[3]="270";
combo1.setModel(new DefaultComboBoxModel(mm) {});
combo1.addMouseMotionListener(this);
combo1.addMouseListener(this);
combo1.setLocation(200,100);combo1.setSize(80,40);
//add(combo1);
System.out.println("LabelX: "+label1X);
System.out.println("LabelY: "+label1Y);
// label1.getParent().repaint();
/*
l.addMouseMotionListener(this);
l.addMouseListener(this);
l1.addMouseMotionListener(this);
l1.addMouseListener(this);
l2.addMouseMotionListener(this);
l2.addMouseListener(this);
l3.addMouseMotionListener(this);
l3.addMouseListener(this);
*/
for (int i=0; i<par.length; i++){
System.out.println("Parametri ricevuti "+par[i]);
}
//TA.addMouseMotionListener(this);
//TA.addMouseListener(this);
B.addMouseMotionListener(this);
B.addMouseListener(this);
P.addMouseMotionListener(this);
P.addMouseListener(this);
CB.addMouseMotionListener(this);
CB.addMouseListener(this);
if (par[6].equalsIgnoreCase("logo1")){
if(logo1Visible){
System.out.println("*******LOGO1: "+immLogo1);
// logo1.setIcon(immLogo1);
logo1.addMouseMotionListener(this);
logo1.addMouseListener(this);
// System.out.println("Imm Logo1: "+immLogo1.getDescription());
logo1.setIcon(ic);
logo1.setLocation(logo1X,logo1Y);
//label1.setLocation(pesoX,pesoY);
logo1.setSize(384,160);
add(logo1);
}
}
if (par[0].equalsIgnoreCase("peso")){
if(pesoVisible){
// label1.setBorder(border);
label1.addMouseMotionListener(this);
label1.addMouseListener(this);
label1.setBorder(BorderFactory.createEtchedBorder());
//label1.setFont(font);
label1.setLocation(label1X,label1Y);
//label1.setLocation(pesoX,pesoY);
label1.setSize(100,20);
// label1.setIcon(immLogo1);
add(getContent());
//add(getCombo());
label1.setName("peso");
add(label1);
repaint();
}
}
if (par[1].equalsIgnoreCase("tara")){
if(taraVisible){
tara.addMouseMotionListener(this);
tara.addMouseListener(this);
//tara.setLocation(49,130);
tara.setBorder(BorderFactory.createEtchedBorder());
tara.setLocation(taraX,taraY);
tara.setSize(100,20);
tara.setName("tara");
add(tara);
repaint();
}
}
if (par[2].equalsIgnoreCase("importo")){
importo.addMouseMotionListener(this);
importo.addMouseListener(this);
importo.setLocation(44,91);importo.setSize(100,20);
importo.setName("IMPORTO");
add(importo);
}
if (par[3].equalsIgnoreCase("bar code")){
if(BarCodeVisible){
barcode.addMouseMotionListener(this);
barcode.addMouseListener(this);
ImageIcon icon = new ImageIcon("/home/max/Downloads/ce.jpg");
barcode.setIcon(icon);
barcode.setLocation(81,223);barcode.setSize(284,177);
add(barcode);
}
}
if (par[4].equalsIgnoreCase("data")){
data.addMouseMotionListener(this);
data.addMouseListener(this);
// data.setLocation(184,177);
data.setSize(100,20);
data.setName("data");
add(data);
}
if (par[5].equalsIgnoreCase("nome prodotto")){
nome.addMouseMotionListener(this);
nome.addMouseListener(this);
nome.setLocation(43,186);nome.setSize(100,20);
add(nome);
}
ImageIcon icon1 = new ImageIcon("/home/max/Downloads/ce.jpg");
logo2.setIcon(icon1);
logo2.setLocation(100, 50);
add(logo2);
//add(B);***********************
B.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
System.out.println("Cliccato");
punti();
setVisible(false);
}
});
/*data.addMouseMotionListener(this);
data.addMouseListener(this);
societa.addMouseMotionListener(this);
societa.addMouseListener(this);
barcode.addMouseMotionListener(this);
barcode.addMouseListener(this);
TA.setLocation(50,50);
TA.setSize(100,100);TA.add("List");
CB.setLocation(120,370);CB.setSize(100,20);
data.setLocation(184,177);data.setSize(100,20);
societa.setLocation(112,41);societa.setSize(100,20);
barcode.setLocation(44,178);barcode.setSize(100,20);*/
B.setLocation(100,150);B.setSize(80,40);
P.setLayout(new BorderLayout());
P.setLocation(100,200);P.setSize(150,150);
P.setBackground(Color.yellow);
P.add(BP,BorderLayout.NORTH);
//P.add(TF,BorderLayout.SOUTH);
//add(TA);add(B);add(P);add(CB);
//add(l);add(l1);add(l2);add(l3);add(label);
//add(label);
//add(data);add(societa);add(tara);add(barcode);
setLocation(500,500);
setSize(b*10,h*10);
setVisible(true);
//show();
}
//mouseDragged
public void mouseDragged(MouseEvent e)
{
Component C=e.getComponent();
if(b1==false){b1=true;Point p=new
Point(e.getPoint());c1=p.x;c2=p.y;}
Point z=new Point(e.getPoint());
Point q=new Point(C.getLocation());
C.setBounds(q.x+(z.x-c1),q.y+(z.y-c2),C.getSize().
width,C.getSize().height);
repaint();
}
//mouseReleased
public void mouseReleased(MouseEvent e)
{
b1=false;
System.out.println("Mouse Rilascaito");
Component C=e.getComponent();
System.out.println("*******++++++++++**********+++++++++Coordinate"+ C);
System.out.println("NOme: "+C.getName());
if(C.getName().equalsIgnoreCase("importo")){
importo.setLocation(C.getX(), C.getY());
importoX=C.getX();
importoY=C.getY();
}
if(C.getName().equalsIgnoreCase("data")){
data.setLocation(C.getX(), C.getY());
}
if(C.getName().equalsIgnoreCase("peso")){
label1X=C.getX();
label1Y=C.getY();
}
C.getX();
C.getY();
//Attenzione qui sotto da controllare!!!!!!!
//pesoX=e.getX();
//pesoY=e.getY();
repaint();
}
//mouseEntered
public void mouseEntered(MouseEvent e)
{
Cursor c1=new Cursor(Cursor.HAND_CURSOR);
setCursor(c1);
}
//mouseExited
public void mouseExited(MouseEvent e)
{
Cursor c2=new Cursor(Cursor.DEFAULT_CURSOR);
setCursor(c2);
}
//mouseClicked
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2) {
Component C=e.getComponent();
System.out.println("*****************+NOme componente: "+C.getName());
System.out.println("Coordinate: "+C.getLocation());
System.out.println("XXX: "+C.getX());
System.out.println("YYY: "+C.getY());
// Configurazione cc=new Configurazione();
// cc.setComponente(C.getName());
// cc.main();
Configurazione cf=new Configurazione();
// cf.setC(C);
cf.main();
//RotLabel rot=new RotLabel();
//rot.main();
System.out.println("\n\nRotazione: "+rot);
System.out.println("Font: "+font);
System.out.println("\n\n");
System.out.println("+++++++++++++++++++++++++++++++Clicccato 2 volte");
/* JFrame frame=new JFrame("Configurazione");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocation(200, 300);
JLabel titolo=new JLabel("Configurazione "+C.getName());
JLabel rotazione=new JLabel("Rotazione");
String [] val=new String [4];
val[0]="0";
val[1]="90";
val[2]="180";
val[3]="270";
JComboBox scelta=new JComboBox(val);
frame.add(titolo);
frame.add(rotazione);
frame.add(scelta);
//3. Create components and put them in the frame.
//...create emptyLabel...
//frame.getContentPane().add(new JLabel("Configurazione"), BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);*/
}
Component C=e.getComponent();
C.getParent().repaint();
repaint();
System.out.println("Coordinate"+ C);
}
//mouseMoved
public void mouseMoved(MouseEvent e)
{
Component C=((Component)e.getSource());
java.util.Random r=new java.util.Random();
java.util.Random g=new java.util.Random();
java.util.Random b=new java.util.Random();
int cr=r.nextInt(255);int cg=g.nextInt(255);
int cb=b.nextInt(255);
Color col=new Color(cr,cg,cb);
//C.setBackground(col);
}
//mousePressed
public void mousePressed(MouseEvent e){}
public class mouse_events{}
public static void punti(){
Inizio in=new Inizio();
System.out.println("\n\n****************Controllo visibilità: ");
System.out.println("Peso: "+pesoVisible);
System.out.println("Tara: "+taraVisible);
System.out.println("Logo1: "+logo1Visible);
System.out.println("***********************************\n\n");
if(logo1Visible){
System.out.println("Parametri Logo1 "+logo1.getLocation());
System.out.println("Parametri X Logo1 "+logo1.getX());
in.setLogo1X(logo1.getX());
//System.out.println("Scrivo app: "+app);
System.out.println("Parametri Y Logo1 "+logo1.getY());
in.setLogo1Y(logo1.getY());
}
if(pesoVisible){
System.out.println("Parametri Label "+label1.getLocation());
System.out.println("Parametri X Label "+label1.getX());
in.setLabelX(label1.getX());
//System.out.println("Scrivo app: "+app);
System.out.println("Parametri Y Label "+label1.getY());
in.setLabelY(label1.getY());
}
if(taraVisible){
System.out.println("Parametri tara "+tara.getLocation());
System.out.println("Parametri X Tara "+tara.getX());
in.setTaraX(tara.getX());
//System.out.println("Scrivo app: "+app);
System.out.println("Parametri Y Tara "+tara.getY());
in.setTaraY(tara.getY());
}
System.out.println("Parametri Bar Code "+barcode.getLocation());
System.out.println("Parametri X Bar code "+barcode.getX()/10);
in.setBarX(barcode.getX()/10);
//System.out.println("Scrivo app: "+app);
System.out.println("Parametri Y Bar Code "+barcode.getY()/10);
in.setBarY(barcode.getY()/10);
// System.out.println("Parametri nome "+nome.getLocation());
// System.out.println("Parametri X nome "+nome.getX()/10);
// in.setNomeX(nome.getX()/10);
//System.out.println("Scrivo app: "+app);
// System.out.println("Parametri Y nome "+nome.getY()/10);
// in.setNomeY(nome.getY()/10);
// System.out.println("Parametri importo "+importo.getLocation());
// System.out.println("Parametri X importo "+importo.getX()/10);
// in.setImportoX(importo.getX()/10);
//System.out.println("Scrivo app: "+app);
// System.out.println("Parametri Y importo "+importo.getY()/10);
// in.setImportoY(importo.getY()/10);
// System.out.println("Parametri data "+data.getLocation());
// System.out.println("Parametri X data"+data.getX()/10);
// in.setDataX(data.getX()/10);
//aa=data.getX();
//System.out.println("Scrivo app: "+app);
// System.out.println("Parametri Y data "+data.getY()/10);
// in.setDataY(data.getY()/10);
}
public void visibile(boolean vis){
P.setVisible(vis);
/*
WindowEvent close = new WindowEvent(FRAME, WindowEvent.WINDOW_CLOSING);
FRAME.dispatchEvent(close);
*/
}
/*
private JComboBox getCombo(){
//final JSlider slider = new JSlider(-180, 180, angle);
String[] val=new String [4];
val[0]="0";
val[1]="90";
val[2]="180";
val[3]="270";
//final JComboBox combo1=new JComboBox(val);
combo1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// angle = Integer.parseInt(combo1.getSelectedItem().toString());//.getValue();
label1.getParent().repaint();
}
});
return combo1;
}*/
private JPanel getContent() {
label1.setFont(new Font("Monospaced", Font.PLAIN, 24));
label1.setBorder(BorderFactory.createEtchedBorder());
JPanel panel = new JPanel(new GridBagLayout());
panel.add(label1, new GridBagConstraints());
return panel;
}
/*
public static void main(String[] args)
{
prova t=new prova("MOUSE EVENT");
t.init();
}*/
public static void refresh(){
//P.getParent().repaint();
P.repaint();
//add(label1);
//repaint();
}
}
谢谢。