import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
public class Demo1 {
/*
* 在主方法中创建窗口对象。
* 画板里的组件和监听要在画板类里面写好。
* 主方法只管创建不同的画板对象往里添加就行了。
*
*/
public static void main(String[] args) {
MyFrame frame = new MyFrame()
MyPanelOne panel1 = new MyPanelOne()
frame.add(panel1)
}
}
class MyFrame extends JFrame { // 主界面
public MyFrame() {
this.setTitle("主界面")
this.setSize(500, 500)
this.setVisible(true)
}
void addPanel(MyPanelOne panel) { // 传递画板的函数
this.setContentPane(panel)
this.setVisible(true)
}
}
class MyPanelOne extends JPanel { // 画板1
public MyPanelOne() {
createButton()
}
private void createButton() {
JButton b1 = new JButton("按钮一")
this.add(b1)
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("hello")
}
})
}
}
class ChildWindos extends JFrame{JButton b1
public ChildWindos(){
super("字体")
this.setResizable(false)
this.setVisible(true)
this.setSize(300,300)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setLayout(null)
b1=new JButton("JBT1")
b1.setSize(100,100)
b1.setLocation(100,100)
this.add(b1)
}
}
你的父窗体怎么加的按钮,子窗体就怎么加,跟那无区别
/**
* 把代码复制到文件,可以运行。
*/
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Component
import java.awt.Font
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.Rectangle
import java.awt.Transparency
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import java.awt.image.BufferedImage
import javax.swing.Icon
import javax.swing.ImageIcon
import javax.swing.JDialog
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTree
import javax.swing.UIManager
import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.TreeCellRenderer
/**
*
* @author beans
*/
public class TreeMain {
public static void main(String[] args) {
new TreeMain().showDialog()
}
public TreeMain() {
}
/**
* 显示窗口
*/
private void showDialog() {
JDialog dialog = new JDialog()
dialog.setBounds(new Rectangle(50, 50, 380, 280))
dialog.setTitle("演示树")
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dialog.setVisible(false)
dialog.dispose()
}
})
dialog.add(this.getPanel(), BorderLayout.CENTER)
dialog.setVisible(true)
}
private JPanel getPanel() {
JPanel panel = new JPanel()
JScrollPane treePanel = new JScrollPane()
treePanel.setViewportView(this.getTree())
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel)
panel.setLayout(layout)
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(treePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
)
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(treePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))
)
panel.add(treePanel, BorderLayout.CENTER)
return panel
}
/**
* 取得树。
*
* @return
*/
private JTree getTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode()
root.add(this.getNode())
root.add(this.getNode())
root.add(this.getNode())
JTree jtree = new JTree(root)
jtree.setRootVisible(false)
jtree.setCellRenderer(new CTreeCellRenderer())
jtree.expandRow(1)
return jtree
}
/**
* 取得树节点。
*
* @return
*/
private DefaultMutableTreeNode getNode() {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new NodeObject(true, "节"))
for (int i = 0 i < 5 i++) {
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(new NodeObject(false, "叶" + i))
node.add(leaf)
}
return node
}
/**
* 树节点和树叶,关联对象。
*/
class NodeObject {
boolean isNode
String name
/**
*
* @param isNode the value of isNode
* @param name the value of name
*/
NodeObject(boolean isNode, String name) {
this.isNode = isNode
this.name = name
}
/**
* 图标
*
* @param isSelect 选中节点时返回不同的图标。
* @return
*/
ImageIcon getIcon(boolean isSelect) {
int wh = 20
BufferedImage image = new BufferedImage(wh, wh, BufferedImage.TYPE_INT_ARGB)
Graphics2D g2 = image.createGraphics()
image = g2.getDeviceConfiguration().createCompatibleImage(wh, wh, Transparency.TRANSLUCENT)
Graphics2D g2d = image.createGraphics()
Font font = new Font("Dialog", Font.PLAIN, wh - 4)
g2d.setFont(font)
g2d.setBackground(Color.WHITE)
g2d.setColor(Color.BLACK)
g2d.drawString(isSelect ? " S " : " N ", 0, wh - 1)
g2d.setColor(this.isNode ? Color.RED : Color.YELLOW)
g2d.drawLine(0, 5, wh, 5)
g2d.drawLine(0, 10, wh, 10)
g2d.drawLine(0, 15, wh, 15)
g2d.dispose()
g2.dispose()
return new ImageIcon(image)
}
String getName() {
return this.name
}
}
/**
* 树渲染器
*/
protected class CTreeCellRenderer extends JLabel implements TreeCellRenderer {
protected Color m_textSelectionColor
protected Color m_textNonSelectionColor
protected Color m_bkSelectionColor
protected Color m_bkNonSelectionColor
protected Color m_borderSelectionColor
protected boolean m_selected
public CTreeCellRenderer() {
m_textSelectionColor = UIManager.getColor("Tree.selectionForeground")
m_textNonSelectionColor = UIManager.getColor("Tree.textForeground")
m_bkSelectionColor = UIManager.getColor("Tree.selectionBackground")
m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground")
m_borderSelectionColor = UIManager.getColor("Tree.selectionBorderColor")
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value
NodeObject obj = (NodeObject) node.getUserObject()
this.setIcon(obj.getIcon(selected))
this.setText(" " + obj.getName() + " ")
this.setForeground(selected ? m_textSelectionColor : m_textNonSelectionColor)
this.setBackground(selected ? m_bkSelectionColor : m_bkNonSelectionColor)
this.m_selected = selected
return this
}
@Override
public void paint(Graphics g) {
Color bColor = this.getBackground()
Icon icon = this.getIcon()
g.setColor(bColor)
int offset = 0
if (icon != null && getText() != null) {
offset = (icon.getIconWidth() + this.getIconTextGap())
}
g.fillRect(offset, 0, this.getWidth() - 1 - offset, this.getHeight() - 1)
if (this.m_selected) {
g.setColor(this.m_borderSelectionColor)
g.drawRect(offset, 0, this.getWidth() - 1 - offset, this.getHeight() - 1)
}
super.paint(g)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)