主要代码: @Override public void paintIcon(Component cmp, Graphics g, int x, int y) { Color lowerColor = new Color(235,255,235); Color highColor = new Color(81,184,77); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); // 画一个渐变圆角矩形 RoundRectangle2D.Float r2d=new RoundRectangle2D.Float(0, 0, btnWidth - 1, btnHeight - 1, 20, 20); Shape clip=g2d.getClip(); g2d.clip(r2d); GradientPaint paint = new GradientPaint(0.0F,0.0F,lowerColor,0.0F,btnHeight,highColor,true); g2d.setPaint(paint); g2d.fillRect(0,0,btnWidth,btnHeight); g2d.setClip(clip); // 画一个黑色边框 paint = new GradientPaint(0,0,new Color(0,0,0), 0,btnHeight-1,new Color(100,100,100)); g2d.setPaint(paint); g2d.drawRoundRect(0,0,btnWidth-1,btnHeight-1,20,20); // 画一个白色边框 paint=new GradientPaint(0,1,new Color(0,0,0,50),0,btnHeight-3,new Color(255,255,255,100)); g2d.setPaint(paint); g2d.drawRoundRect(1,1,btnWidth-3,btnHeight-3,18,18); FontMetrics fm = g2d.getFontMetrics(boldFont); int textWidth = fm.stringWidth(btnText); int textAscent = fm.getAscent(); g2d.setColor(hoverColor); g2d.setFont(boldFont); g2d.drawString(btnText, (btnWidth - textWidth) / 2, (btnHeight + textAscent) / 2 - 2); }
2021-11-20 15:16:41 24KB java
1
java按钮jbutton透明设置 不要用 setOpaque(false)的方法
2021-09-24 11:16:16 114B java button 透明
1
swing 中JTABLE中添加控件的例子 可以引申出添加很多控件
2021-08-06 23:50:29 4KB JATBLE jbutton sWING
1
java swing JButton 圆角按钮 带变色(有注释,简单易懂适用于初学者 程序可直接运行)
2021-07-24 01:51:34 3KB java swing JButton 圆角按钮
1
使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact
2020-01-03 11:23:10 4KB Table JButton 按钮
1