java.awt.Container.add()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(555)

本文整理了Java中java.awt.Container.add()方法的一些代码示例,展示了Container.add()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Container.add()方法的具体详情如下:
包路径:java.awt.Container
类名称:Container
方法名:add

Container.add介绍

[英]Appends the specified component to the end of this container. This is a convenience method for #addImpl.

Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component. If multiple components are being added, you can improve efficiency by calling validate only once, after all the components have been added.
[中]将指定的组件追加到此容器的末尾。这是一种方便的#addImpl方法。
注意:如果组件已添加到已显示的容器中,则必须对该容器调用validate以显示新组件。如果要添加多个组件,则在添加所有组件后,只需调用validate一次即可提高效率。

代码示例

代码示例来源:origin: libgdx/libgdx

public void run () {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(480, 320);
    frame.setLocationRelativeTo(null);
    JPanel panel = new JPanel();
    frame.getContentPane().add(panel);
    panel.add(new NewSlider(200, 100, 500, 0.1f, 150, 300));
    frame.setVisible(true);
  }
});

代码示例来源:origin: stackoverflow.com

$cat HelloWorldSwing.java
 package start;
 import javax.swing.*;
 public class HelloWorldSwing {
   public static void main(String[] args) {
     //Create and set up the window.
     JFrame frame = new JFrame("HelloWorldSwing");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     JLabel label = new JLabel("Hello World");
     frame.add(label);
     //Display the window.
     frame.pack();
     frame.setVisible(true);
   }
 }
 class Dummy {
   // just to have another thing to pack in the jar
 }

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

public void show(){
  frame = new JFrame("HDR View");
  label = new JLabel(new ImageIcon(image));
  frame.getContentPane().add(label);
  frame.setLayout(new FlowLayout());
  frame.pack();
  frame.setVisible(true);
}

代码示例来源:origin: stackoverflow.com

import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.*;

public class FrameTest {
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    cp.add(new JComponent() {
      public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(10));
        g2.draw(new Line2D.Float(30, 20, 80, 90));
      }
    });
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}

代码示例来源:origin: stackoverflow.com

this.panel = new JPanel();
this.panel.setLayout(new FlowLayout());
add(panel, BorderLayout.CENTER);
JButton button = new JButton("CLICK HERE");
add(button, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
this.panel.add(new JButton("Button"));
this.panel.revalidate();
validate();

代码示例来源:origin: stackoverflow.com

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class Rb  extends JFrame {
Rb (){
   JRadioButton male = new JRadioButton("male");
   JRadioButton female = new JRadioButton("Female");
   ButtonGroup bG = new ButtonGroup();
   bG.add(male);
   bG.add(female);
   this.setSize(100,200);
   this.setLayout( new FlowLayout());
   this.add(male);
   this.add(female);
   male.setSelected(true);
   this.setVisible(true);
 }
public static void main(String args[]){
  Rb j = new Rb();
}

代码示例来源:origin: stackoverflow.com

import java.awt.event.*;
import javax.swing.*;

public class YourDialog extends JDialog implements ActionListener {

 JButton button;

 public YourDialog() {
   button = new JButton("Close");
   button.addActionListener(this);
   add(button);
   pack();
   setVisible(true);
 }

 public void actionPerformed(ActionEvent e) {
   dispose();
 }
}

代码示例来源:origin: stackoverflow.com

import javax.swing.JFrame;
import javax.swing.JLabel;

public class AnotherJFrame extends JFrame
{
  public AnotherJFrame()
  {
    super("Another GUI");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(new JLabel("Empty JFrame"));
    pack();
    setVisible(true);
  }
}

代码示例来源:origin: libgdx/libgdx

public void run () {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(480, 320);
    frame.setLocationRelativeTo(null);
    JPanel panel = new JPanel();
    frame.getContentPane().add(panel);
    panel.add(new Slider(200, 100, 500, 0.1f, 150, 300));
    frame.setVisible(true);
  }
});

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
frame.setVisible(true);

代码示例来源:origin: stanfordnlp/CoreNLP

public static void main(String[] args) throws IOException {
 TreeJPanel tjp = new TreeJPanel();
 // String ptbTreeString1 = "(ROOT (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (NN test))) (. .)))";
 String ptbTreeString = "(ROOT (S (NP (NNP Interactive_Tregex)) (VP (VBZ works)) (PP (IN for) (PRP me)) (. !))))";
 if (args.length > 0) {
  ptbTreeString = args[0];
 }
 Tree tree = (new PennTreeReader(new StringReader(ptbTreeString), new LabeledScoredTreeFactory(new StringLabelFactory()))).readTree();
 tjp.setTree(tree);
 tjp.setBackground(Color.white);
 JFrame frame = new JFrame();
 frame.getContentPane().add(tjp, BorderLayout.CENTER);
 frame.addWindowListener(new WindowAdapter() {
  @Override
  public void windowClosing(WindowEvent e) {
   System.exit(0);
  }
 });
 frame.pack();
 frame.setVisible(true);
 frame.setVisible(true);
}

代码示例来源:origin: stackoverflow.com

public static void main(String args[]) {
  FrameTestBase t = new FrameTestBase();
  t.add(new JComponent() {
    public void paintComponent(Graphics g) {
      String str = "hello world!";
  t.setDefaultCloseOperation(EXIT_ON_CLOSE);
  t.setSize(400, 200);
  t.setVisible(true);

代码示例来源:origin: stackoverflow.com

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Annoying {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Hello!!");

    // Set's the window to be "always on top"
    frame.setAlwaysOnTop( true );

    frame.setLocationByPlatform( true );
    frame.add( new JLabel("  Isn't this annoying?") );
    frame.pack();
    frame.setVisible( true );
  }
}

代码示例来源:origin: libgdx/libgdx

public void run () {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(480, 320);
    frame.setLocationRelativeTo(null);
    JPanel panel = new JPanel();
    frame.getContentPane().add(panel);
    panel.add(new NewSlider(200, 100, 500, 0.1f, 150, 300));
    frame.setVisible(true);
  }
});

代码示例来源:origin: stackoverflow.com

package com.stackoverflow.test;

import java.net.URL;
import javax.swing.*;  // Wild carded for brevity. 
            // Actual code imports single classes
public class Main {
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
      public void run() {
        URL url = Main.class.getResource(
                   "/resources/stackoverflow.png");
        ImageIcon icon = new ImageIcon(url);
        JFrame frame = new JFrame();
        frame.add(new JLabel(icon));
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    });
  }
}

代码示例来源:origin: libgdx/libgdx

public void run () {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(480, 320);
    frame.setLocationRelativeTo(null);
    JPanel panel = new JPanel();
    frame.getContentPane().add(panel);
    panel.add(new Slider(200, 100, 500, 0.1f, 150, 300));
    frame.setVisible(true);
  }
});

代码示例来源:origin: stackoverflow.com

package temp;
import java.awt.EventQueue;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new NewJPanel());
        f.pack();
        f.setVisible(true);
      }
    });
  }
}

代码示例来源:origin: loklak/loklak_server

/**
 * show the image as JFrame on desktop
 */
public void show() {
  JLabel label = new JLabel(new ImageIcon(this.image));
  JFrame f = new JFrame();
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.getContentPane().add(label);
  f.pack();
  f.setVisible(true);
}

代码示例来源:origin: stackoverflow.com

JFrame f = new JFrame("Frame " + ii);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
String s =
  "os.name: " + System.getProperty("os.name") +
  "\nos.version: " + System.getProperty("os.version");
f.add(new JTextArea(s,3,28));  // suggest a size
f.pack();
f.setVisible(true);

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

private static void createWindowForPanel(AwtPanel panel, int location){
  JFrame frame = new JFrame("Render Display " + location);
  frame.getContentPane().setLayout(new BorderLayout());
  frame.getContentPane().add(panel, BorderLayout.CENTER);
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
      if (++panelsClosed == 2){
        app.stop();
      }
    }
  });
  frame.pack();
  frame.setLocation(location, Toolkit.getDefaultToolkit().getScreenSize().height - 400);
  frame.setVisible(true);
}

相关文章

微信公众号

最新文章

更多

Container类方法